我有这样的代码:
Arrays.asList(1L, 2L, 3L, 10L, 20L, 30L, 100L)
.stream()
.map(Bytes::fromMegaBytes) // Function<Long, Bytes>
.map(FileUtils::generateTempFileRunEx) // Function<Bytes, Path>
.flatMap(path -> parameters.getAllocatedCredits() // Map<Object, Integer>
.keySet()
.stream()
.map(group -> Pair.of(group, path))
)
.collect(Collectors.toList())
.forEach(item -> { // item should be Pair<Object,Path>
System.out.println(item.getKey() + ": " + item.getValue());
// The method getKey() is undefined for the type Object
// The method getValue() is undefined for the type Object
});我知道Javac或ECJ (在这种情况下)在猜测方法类型参数时可能出错,在这种情况下,我们被迫告诉编译器哪种类型:
.flatMap(path -> parameters.getAllocatedCredits()
.keySet()
.stream()
.<Pair<Object,Path>> map(group -> Pair.of(group, path))为什么在这种特殊情况下,ECJ不正确地猜测类型,而这似乎是一个简单的案例?
编辑:在javac (使用maven)上测试后更新了我的答案,并看到了它的工作原理。
编辑(2):重构代码到此工作:
.flatMap(path -> {final Stream<Pair<Object, Path>> w = parameters.getAllocatedCredits()
.keySet()
.stream()
.map(group -> Pair.of(group, path));
return w;
}注意: Pair取自commons,并实现了Map.Entry。
发布于 2018-12-06 19:50:53
若要关闭此循环:
报道的ecj中的缺陷被发现是bug 444891的传递性复制。后者实现了一个JLS的变化,它是在Java8GA之后提出的,并将于2015-02-16年解决。根据JLS的更改,可以修复ecj错误,并为Eclipse4.5 (Mars)发布。
https://stackoverflow.com/questions/25531827
复制相似问题