By comparing two hashmap & lt; String, Integer & gt; And get the key with the lowest integer value in java

advertisements

I am trying to compare two hashmaps of type <String, Integer> and trying to get the result based on the smallest value of integer from first map and the biggest value from second map. Eg:

hashmap1:

Key value
 A    2
 B    2
 C    4

hashmap2:
key value
 A   10
 B   20
 C    5

The result would be "B 2"


Here is an alternative method using the stream API:

String key = map.entrySet().stream()
    .min((o1, o2) -> o1.getValue() - o2.getValue()).get().getKey();

though I admit it is quite verbose.

key stores the key that corresponds to the lowest value.