Find key with the maximum value in a Map in Java
This post will discuss how to find the key(s) having the maximum value in a Map in Java.
1. Using for-loop
The idea is to iterate through all the entries of the map and keep track of the entry with the maximum value so far. The following example demonstrates this using a for-loop. Note that if multiple keys have the same maximum value, the code returns the first key it finds with the maximum value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<>(); hm.put('A', 1); hm.put('B', 2); hm.put('C', 3); hm.put('D', 4); Map.Entry<Character, Integer> maxEntry = null; for (Map.Entry<Character, Integer> entry : hm.entrySet()) { if (maxEntry == null || entry.getValue() > maxEntry.getValue()) { maxEntry = entry; } } System.out.println(maxEntry); } } |
Output:
D=4
You can also iterate over the map using the keySet()
method and find the key with the maximum value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<>(); hm.put('A', 1); hm.put('B', 2); hm.put('C', 3); hm.put('D', 4); Character maxKey = null; for (Character key : hm.keySet()) { if (maxKey == null || hm.get(key) > hm.get(maxKey)) { maxKey = key; } } System.out.println(maxKey); } } |
Output:
D
2. Using Stream API
With Java 8 and later, you can use Stream API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Comparator; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<>(); hm.put('A', 1); hm.put('B', 2); hm.put('C', 3); hm.put('D', 4); Map.Entry<Character, Integer> maxEntry = hm.entrySet().stream() .max(Comparator.comparing(Map.Entry::getValue)) .orElse(null); System.out.println(maxEntry); } } |
Output:
D=4
You can also get a stream of keys contained in the map and find the maximum-valued key:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Comparator; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<>(); hm.put('A', 1); hm.put('B', 2); hm.put('C', 3); hm.put('D', 4); Character maxKey = hm.keySet().stream() .max(Comparator.comparing(hm::get)) .orElse(null); System.out.println(maxKey); } } |
Output:
D
Here’s another variation of the above approach using Map.Entry.comparingByValue
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Collections; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<>(); hm.put('A', 1); hm.put('B', 2); hm.put('C', 3); hm.put('D', 4); Map.Entry<Character, Integer> maxEntry = Collections.max(hm.entrySet(), Map.Entry.comparingByValue()); System.out.println(maxEntry); } } |
Output:
D=4
3. Get all Mappings
To fetch all keys having the maximum value, first, find the maximum value in the map, then iterate over the map’s entries, and filter all entries with the maximum value. Here’s the complete code:
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 |
import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<>(); hm.put('A', 1); hm.put('B', 4); hm.put('C', 3); hm.put('D', 4); int maxValue = Collections.max(hm.values()); List<Character> maxValueKeys = hm.entrySet().stream() .filter(entry -> entry.getValue() == maxValue) .map(Map.Entry::getKey) .collect(Collectors.toList()); System.out.println(maxValueKeys); } } |
Output:
[B, D]
Here’s a version without using the Stream API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.*; public class Main { public static void main(String[] args) { Map<Character, Integer> hm = new HashMap<>(); hm.put('A', 1); hm.put('B', 4); hm.put('C', 3); hm.put('D', 4); int maxValue = Collections.max(hm.values()); List<Character> maxValueKeys = new ArrayList<>(); for (Map.Entry<Character, Integer> entry : hm.entrySet()) { if (entry.getValue() == maxValue) { maxValueKeys.add(entry.getKey()); } } System.out.println(maxValueKeys); } } |
Output:
[B, D]
That’s all about finding the key(s) having the maximum value in a Map in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)