0%

How to find the Entry with largest Value in a Java Map

How to find the Entry with largest Value in a Java Map

  • Last Updated : 13 Sep, 2021

Read

Discuss

Given a map in Java, the task is to find out the entry in this map with the highest value.

Illustration:

1
2
3
4
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:

  1. Simple iterative approach via for each loop
  2. Using max() method from Collections class
  3. 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:

  1. Iterate the map entry by entry
  2. Store the first entry in a reference variable to compare to initially.
  3. If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry.
  4. Repeat this process for all the entries in the map.
  5. In the end, the reference variable has the required entry with the highest value in the map.
  6. Print this entry
1
2
for (Map.Entry entry : map.entrySet()) 
{ // Operations }

Example:

  • Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Java program to Find Entry
// with the Highest Value in Map
// Using Comparators in Map interface

// Importing all utility classes
import java.util.*;

// Main class
class GFG {

// Method 1
// Find the entry with highest value
public static <K, V extends Comparable<V> >
Map.Entry<K, V>
getMaxEntryInMapBasedOnValue(Map<K, V> map)
{

// To store the result
Map.Entry<K, V> entryWithMaxValue = null;

// Iterate in the map to find the required entry
for (Map.Entry<K, V> currentEntry :
map.entrySet()) {

if (
// If this is the first entry, set result as
// this
entryWithMaxValue == null

// If this entry's value is more than the
// max value Set this entry as the max
|| currentEntry.getValue().compareTo(
entryWithMaxValue.getValue())
> 0) {

entryWithMaxValue = currentEntry;
}
}

// Return the entry with highest value
return entryWithMaxValue;
}

// Method 2
// To print the map
public static void print(Map<String, Integer> map)
{

System.out.print("Map: ");

// If map does not contain any value
if (map.isEmpty()) {

System.out.println("[]");
}
else {
System.out.println(map);
}
}

// Method 3
// Main driver method
public static void main(String[] args)
{

// Creating a Map
// Declaring object of string and integer type
Map<String, Integer> map = new HashMap<>();

// Inserting elements in the Map object
// using put() method
// Custom input element addition
map.put("ABC", 10);
map.put("DEF", 30);
map.put("XYZ", 20);

// Calling method 2 to
// print the map
print(map);

// Calling method 1 to
// find the entry with highest value and
// print on the console
System.out.println(
"Entry with highest value: "
+ getMaxEntryInMapBasedOnValue(map));
}
}

Output:

1
2
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Java Program to Find Entry with largest Value in Map
// Using max() method from Collections class

// Importing required classes
import java.util.*;

// main class
class GFG {

// Main driver method
public static void main(String[] args)
{

// Creating HashMap
// Declaring objects of string and integer type
HashMap<Integer, Integer> map
= new HashMap<Integer, Integer>();

// Inserting elements in the Map
// using put() method

// Custom input addition
map.put(1, 4);
map.put(2, 5);
map.put(3, 7);
map.put(4, 2);

// Using Collections.max() method returning max
// value in HashMap and storing in a integer
// variable
int maxValueInMap = (Collections.max(map.values()));

// Iterate through HashMap
for (Entry<Integer, Integer> entry :
map.entrySet()) {

if (entry.getValue() == maxValueInMap) {

// Print the key with max value
System.out.println(entry.getKey());
}
}
}
}

Output:

1
3

Method 3: Using the concept of Streams introduced in Java 8

  • Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Java Program to Find Entry with largest Value in Map
// Using concept of Streams
import java.util.stream.*;

// Main class
class GFG {

// Method 1
public static void main(String[] args)
{

// Entries in our map
Map<String, String> map = new HashMap<>();

map.put("A", "23");
map.put("F", "43");
map.put("C", "56");
map.put("Z", "04");

// Printing the largest value in map by
// calling above method
System.out.print(map.maxUsingStreamAndLambda());
}

// Method 2
public <String, String>
extends Comparable<String> > maxUsingStreamAndLambda(
Map<String, String> map)
{

// Using lambda operation over streams
Map<String, String> maxEntry
= map.entrySet().stream().max(
(String e1, String e2)
-> e1.getValue().compareTo(
0e2.getValue()));

// Returning the maximum element from map
return maxEntry.get().getValue();
}
}

Output:

1
C

Welcome to my other publishing channels