I wanna find the maximum value in a hashMap<string, integer> , please how do i iterate through the hashMap? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I wanna find the maximum value in a hashMap<string, integer> , please how do i iterate through the hashMap?

Problem with HashMap

28th Jan 2021, 8:30 AM
Jeffrey Solomon Maluleka
Jeffrey Solomon Maluleka - avatar
3 Answers
+ 6
HashMaps are not iterable, what you can do is convert the hashmap into something that you can iterate. The HashMap<String, Integer>.entrySet() returns a Set<Map.Entry<String, Integer>> that you can iterate over. A Map.Entry<String, Integer> is just a single key-value pair. To retrieve the key, use .getKey() and for the value .getValue().
28th Jan 2021, 8:49 AM
jtrh
jtrh - avatar
+ 2
The easiest way would be to use a lambda expression
28th Jan 2021, 11:50 PM
Anastacia Ru
Anastacia Ru - avatar
0
Something Like integer maxV=Integer.MIN_VALUE;; outputMap.forEach((k, v) -> { maxV=Math.max(maxV,v); } one way to do it
28th Jan 2021, 11:54 PM
Anastacia Ru
Anastacia Ru - avatar