Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the Biggest number in HashSet/HashMap java

I would like to find the biggest number in HashSet and HashMap. Say I have the number [22,6763,32,42,33] in my HashSet and I want to find the largest number in my current HashSet..how would i do this? and Same thing for the HashMap as well. I hope you can help me with it. Thank you.

like image 509
user2064467 Avatar asked Sep 06 '25 03:09

user2064467


2 Answers

You can use Collections.max(Collection) to find the maximum element out of any collection. Similarly, for a HashMap, you can use the same method on its keySet() or values(), depending upon whether you want maximum key, or maximum value.

Also, if you want as such, you can use a TreeSet and TreeMap instead, that stores the elements in sorted key order.

like image 121
Rohit Jain Avatar answered Sep 08 '25 01:09

Rohit Jain


try

    int max = Collections.max(set);
    int maxKey = Collections.max(map.keySet());
    int maxValue Collections.max(map.values());
like image 35
Evgeniy Dorofeev Avatar answered Sep 07 '25 23:09

Evgeniy Dorofeev