Order for a pair of key values

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 figure out the maximum pair from a map, where the objects of these classes are respectively key and value pairs. I also have com.google.common.collect.Ordering<ValueClass> that uses multiple comparators. I can easily find out the maximum values ​​using this ordering, but I'm interested in the maximum value key.

I can write a specific implementation in which I can track my key by value in a loop and use ordering to compare values ​​(similar to the usual methods of finding the maximum value), but I want to know if we already have such a case, Guava or any other library?

+5
source share
3 answers

You say guava or any other library, and that’s right with the Java 8 threads. If your instance of Ordering<ValueClass> is called ordering :

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

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

This is possible since guava ordering implements java.util.Comparator , so you can pass it as an argument to comparing .

+5
source

I recommend you do the following:

Change com.google.common.collect.Ordering<ValueClass> to com.google.common.collect.Ordering<Map.Entry<KeyClass, ValueClass>> and change several Comparator<ValueClass> that are used instead of Map.Entry#getValue .

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

I can easily find out the maximum values ​​using this ordering, but I'm interested in the maximum value key.

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

+1
source

Yes. Using 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 execute the hascode() and equals() contract on the map. Decision: -

  • we know that HashCode used for saving in the Bucket . Assume a HashCode as a row number in a 2-D matrix , and each Row has a linked entry[key,value] list.

  • implements hashcode() such that for each entry[key,value] you get a different `bucket no (row no).

  • Implement the equals() method to check for equality in each entry in the [key, value] column.

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

  • Please refer to the documents below for a better understanding of the solution: - doc1 doc2

+1
source

Source: https://habr.com/ru/post/1269604/


All Articles