Java 在 Map 上使用 Map.Entry 未编译

标签 java dictionary

我收到一个错误,我似乎无法自行修复,这可能是一个愚蠢的错误,但我无法看到它。

Map<TemplateBean, Map<String, String>> templateMap = new HashMap<>();
//some code that fills up the map
int biggestSize = 0;
Map<String, String> biggestValues = null;
for (Map.Entry<TemplateBean, Map<String, String>> entry : templateMap.values()) {
    Map<String, String> currentValues = entry.getValue();
    int currentSize = currentValues.size();
    if (currentSize > biggestSize) {
        biggestSize = currentSize;
        biggestValues = currentValues;
    }
}
if (biggestValues != null) {
    values = biggestValues;
}

在 for 循环中出现此错误:

incompatible types
  required: Entry<TemplateBean,Map<String,String>>
  found:    Map<String,String>

但是我很确定我的理解是正确的,我对迭代 map 或其他任何东西并不陌生,但现在仍然是星期二早上。

问候。

最佳答案

更改此行 -

for (Map.Entry<TemplateBean, Map<String, String>> entry: 
            templateMap.values())

至 -

for (Map.Entry<TemplateBean, Map<String, String>> entry: 
            templateMap.entrySet())

查看 JavaDoc .

关于Java 在 Map 上使用 Map.Entry 未编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17806507/

相关文章:

python - 尝试在 dict 上调用方法,得到 AttributeError : 'dict' object attribute 'update' is read-only

java - 使用 3x3 高斯核模糊图像?

java - spring-boot 更新到 1.3.1 后 webapp 无法启动

c++ - STL map 不会删除

python - 如何对字典进行排序?

python - 从列表元素形成字典

来自 mobypos.txt 文件的 Python 字典

Java Web Start 导致执行缓慢

java - 时间戳到字符串到日期时间问题

java - 在 Android 中共享 Intent 不向任何应用程序共享图像(弹出窗口后)