我有一个 HashMap ,如下所示;
HashMap<ArrayList<Integer>,ArrayList<String>>
我想从第二个 ArrayList(值)中找到具有最大 length() 的 ArrayList
最有效的方法是什么?
最佳答案
您可以遍历 map 的 values()
:
ArrayList<String> max = null;
for (ArrayList<String> list : map.values()) {
if (max == null || list.size() > max.size())
max = list;
}
获取与最大值关联的键:
ArrayList<Integer> maxKey = null;
int maxLen = 0;
for (Entry<ArrayList<Integer>, ArrayList<String>> e : map.entrySet()) {
int len = e.getValue().size();
if (maxKey == null || len > maxLen) {
maxKey = e.getKey();
maxLen = len;
}
}
关于java - 在 Hashmap<Arraylist,Arraylist> 中找到最大值的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18065738/