我正进入(状态
Cannot infer type arguments for java.util.HashMap<>
对于以下代码
class Test {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "x");
map.put(2, "y");
map.put(3, "x");
map.put(4, "z");
//the following line has error
Map<String, ArrayList<Integer>> reverseMap = new java.util.HashMap<>(map.entrySet().stream()
.collect(Collectors.groupingBy(Map.Entry::getValue)).values().stream()
.collect(Collectors.toMap(item -> item.get(0).getValue(),
item -> new ArrayList<>(item.stream().map(Map.Entry::getKey).collect(Collectors.toList()))));
System.out.println(reverseMap);
}
}
出了什么问题,谁能给我解释一下?我检查了正确的导入,发现我正在导入 java.util.hashmap 而不是其他。讨厌的错误仍然困扰着我。
原文由 Chaitanya Uttarwar 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是
ecj
(eclipse 编译器)中的一个错误,您可以解决它并添加更多类型信息:看看我是如何添加的
ArrayList<Integer>
。它用
javac-8 and 9
编译得很好。顺便说一句,似乎有一种更简单的方法可以做事: