Find the Biggest number in HashSet/HashMap java

后端 未结 8 2181
我在风中等你
我在风中等你 2020-12-29 04:48

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 Ha

8条回答
  •  不思量自难忘°
    2020-12-29 05:16

    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.

提交回复
热议问题