在 HashSet/HashMap java中找到最大的数

Find the Biggest number in HashSet/HashMap java(在 HashSet/HashMap java中找到最大的数)

本文介绍了在 HashSet/HashMap java中找到最大的数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 HashSet 和 HashMap 中找到最大的数.假设我的 HashSet 中有数字 [22,6763,32,42,33],我想在当前的 HashSet 中找到最大的数字..我该怎么做?HashMap 也一样.我希望你能帮助我.谢谢你.

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.

推荐答案

你可以使用Collections.max(Collection) 找出任何集合中的最大元素.同样,对于 HashMap,您可以在其 keySet() 或 values(),取决于您想要最大键还是最大值.

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.

此外,如果您愿意,可以使用 TreeSet 和 TreeMap 相反,它以排序键顺序存储元素.

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

这篇关于在 HashSet/HashMap java中找到最大的数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 HashSet/HashMap java中找到最大的数

基础教程推荐