How to find the Entry with largest Value in a Java Map
Given a map in Java, the task is to find out the entry in this map with the highest value.
Illustration:
Input : Map = {ABC = 10, DEF = 30, XYZ = 20}
Output : DEF = 30
Input : Map = {1 = 40, 2 = 30, 3 = 60}
Output : 3 = 60
Methods: There can be several approaches to achieve the goal that are listed as follows:
- Simple iterative approach via for each loop
- Using max() method from Collections class
- Using the concept of Streams introduced in Java 8
Now we will discuss the above-proposed methods alongside procedure and implementing them via clean java programs.
Method 1: Using iterative approach for each loop
Approach:
- Iterate the map entry by entry
- Store the first entry in a reference variable to compare to initially.
- If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry.
- Repeat this process for all the entries in the map.
- In the end, the reference variable has the required entry with the highest value in the map.
- Print this entry
for (Map.Entry entry : map.entrySet())
{ // Operations }
Example:
Java
import java.util.*;
class GFG {
public static <K, V extends Comparable<V> >
Map.Entry<K, V>
getMaxEntryInMapBasedOnValue(Map<K, V> map)
{
Map.Entry<K, V> entryWithMaxValue = null ;
for (Map.Entry<K, V> currentEntry :
map.entrySet()) {
if (
entryWithMaxValue == null
|| currentEntry.getValue().compareTo(
entryWithMaxValue.getValue())
> 0 ) {
entryWithMaxValue = currentEntry;
}
}
return entryWithMaxValue;
}
public static void print(Map<String, Integer> map)
{
System.out.print( "Map: " );
if (map.isEmpty()) {
System.out.println( "[]" );
}
else {
System.out.println(map);
}
}
public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<>();
map.put( "ABC" , 10 );
map.put( "DEF" , 30 );
map.put( "XYZ" , 20 );
print(map);
System.out.println(
"Entry with highest value: "
+ getMaxEntryInMapBasedOnValue(map));
}
}
|
Output:
Map: {ABC=10, DEF=30, XYZ=20}
Entry with highest value: DEF=30
Method 2: Using max() method from Collections class without lambda expression
Example:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
HashMap<Integer, Integer> map
= new HashMap<Integer, Integer>();
map.put( 1 , 4 );
map.put( 2 , 5 );
map.put( 3 , 7 );
map.put( 4 , 2 );
int maxValueInMap = (Collections.max(map.values()));
for (Entry<Integer, Integer> entry :
map.entrySet()) {
if (entry.getValue() == maxValueInMap) {
System.out.println(entry.getKey());
}
}
}
}
|
Output:
3
Method 3: Using the concept of Streams introduced in Java 8
Java
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
Map<String, String> map = new HashMap<>();
map.put( "A" , "23" );
map.put( "F" , "43" );
map.put( "C" , "56" );
map.put( "Z" , "04" );
System.out.print(map.maxUsingStreamAndLambda());
}
public <String, String>
extends Comparable<String> > maxUsingStreamAndLambda(
Map<String, String> map)
{
Map<String, String> maxEntry
= map.entrySet().stream().max(
(String e1, String e2)
-> e1.getValue().compareTo(
0e2.getValue()));
return maxEntry.get().getValue();
}
}
|
Output:
C
Last Updated :
13 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...