我收到一个错误,我似乎无法自行修复,这可能是一个愚蠢的错误,但我无法看到它。
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/