我如何理解该程序的输出?当我执行下面的程序时,我看到下面的输出,但无法理解它是什么。它是collect()
:
import java.util.List;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;
class example1 {
public static void main(String []args){
List <Integer> list = Arrays.asList(new Integer []{3,4,5,6,7,8,9});
HashMap<Integer, List<Integer>> map =
list.parallelStream().collect(Collectors.groupingByConcurrent(i->i%2 == 0?1:2)); /* what's problem here */
System.out.println(map.get(0));
}
}
输出
java: incompatible types: inference variable R has incompatible bounds equality constraints:
java.util.concurrent.ConcurrentMap<java.lang.Integer,java.util.List<java.lang. Integer>> upper bounds:
java.util.concurrent.ConcurrentHashMap<java.lang.Integer,java.util.List<java.lang.Integer>>,java.lang.Object
答案 0 :(得分:1)
问题是您为groupingByConcurrent的返回类型声明了错误的类型。从
更改您的声明HashMap<Integer, List<Integer>> map
到
Map<Integer, List<Integer>> map