Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering for key value pairs

I have the following two classes:

class KeyClass {
    private prop1;
    private prop2;

    hashcode() {
    //implemented properly
    }

    equals() {
    //implemented properly
    }
}

class ValueClass {
    private prop1;
    private prop2;

    hashcode() {
    //implemented properly
    }

    equals() {
    //implemented properly
    }
}

I am trying to find out the max pair from a map where objects of these classes are key and value pairs respectively. I also have an com.google.common.collect.Ordering<ValueClass> which uses multiple comparators. I can easily find out the max of values using this ordering, but what I am interested into is the key of the max value.

I can write a certain implementation wherein I can keep track of my key w.r.t the value in a loop and use the ordering to compare values (similar to the conventional way of finding a max value) but I want to know if we already have such case handled by Guava or any other library?

like image 976
Mukund Jalan Avatar asked Jul 07 '17 12:07

Mukund Jalan


People also ask

How do you generate key-value pairs?

If we suppose that your keyvaluepair has as a key a string and as a value an int, then you could try this one: clsName. PropertyName = new KeyValuePair<string, int>("keyName", 2); You don't need to use the any Add method.

Which class do you use to store a set of key-value pairs that are sorted by the key and not inserted randomly?

LinkedHashMap in Java is used to store key-value pairs very similar to HashMap class. Difference is that LinkedHashMap maintains the order of elements inserted into it while HashMap is unordered.


3 Answers

You say guava or any other library and that's straightforward with Java 8 streams. If your Ordering<ValueClass> instance is called ordering:

Entry<KeyClass, ValueClass> maxEntry = map.entrySet().stream()
        .max(Comparator.comparing(Entry::getValue, ordering))
        .orElse(null);

Add .map(Entry::getKey) before orElse to get just the key.

The above is possible since guava's Ordering implements java.util.Comparator so you can pass it as argument to comparing.

like image 193
Manos Nikolaidis Avatar answered Nov 15 '22 08:11

Manos Nikolaidis


I recommend you do the following:

Change your com.google.common.collect.Ordering<ValueClass> to com.google.common.collect.Ordering<Map.Entry<KeyClass, ValueClass>> and modify the multiple Comparator<ValueClass> that are used to use Map.Entry#getValue instead.

Thus, the maximum ValueClass will be equivalent to the maximum Map.Entry<KeyClass, ValueClass>.

I can easily find out the max of values using this ordering, but what I am interested into is the key of the max value.

Now, you can simply use Map.Entry#getKey to grab the key of the maximum value/entry.

like image 29
Jacob G. Avatar answered Nov 15 '22 08:11

Jacob G.


yes. using the Bidi Map :- Bidi Map

Download jar

How to use.

Example:-

BidiMap bidiMap = new DualHashBidiMap( );
bidiMap.put( "il", "Illinois" );
bidiMap.put( "az", "Arizona" );
bidiMap.put( "va", "Virginia" );
// Retrieve the key with a value via the inverse map
String vaAbbreviation = bidiMap.inverseBidiMap( ).get( "Virginia" );

// Retrieve the value from the key
String illinoisName = bidiMap.get( "il" );

Using the same approach you just need to implement the hascode() and equals() contract of a Map. Solution :-

  1. we know that HashCode is used to Store in Bucket. Assume hashcode as row number in 2-D matrix and each Row has the linked List of entry[key,value].

  2. implement the hashcode() such that for each entry[key,value] you will get different `bucket no(row no).

  3. Implement equals() method to check the equality in each bucket's entry[key,value].

  4. Complexity :- if you assign each entry[key, value] to the different bucket then SEARCHING and ADDING complexity will be o(1).

  5. please refer the below documents for better understanding of solution:-doc1 doc2

like image 30
Hasnain Ali Bohra Avatar answered Nov 15 '22 07:11

Hasnain Ali Bohra