java - 在 Hashmap<Arraylist,Arraylist> 中找到最大值的最佳方法

标签 java algorithm data-structures hashmap

我有一个 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/

相关文章:

algorithm - 处理和理解句子

python - 如何确定完全打破连接环的最小数量?

c++ - 关于使用哪种数据结构来快速搜索 C++ 的建议

java - 4.23 Java 中匹配数字之前的实验倒计时

javascript - 在javascript中查找大小为2的集合中的所有分区

arrays - 数据结构的选择

algorithm - Geohash 边界框搜索

java - JSP 表中的拆分数组值

java - 在 android 中为类编写 Parcelable 时出现 StackOverflowError

java - 如何根据ViewPager中当前的ImageView更新父Activity中的TextView