不要理解collect()的输出

时间:2017-10-04 14:18:50

标签: java

我如何理解该程序的输出?当我执行下面的程序时,我看到下面的输出,但无法理解它是什么。它是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

1 个答案:

答案 0 :(得分:1)

问题是您为groupingByConcurrent的返回类型声明了错误的类型。从

更改您的声明
HashMap<Integer, List<Integer>> map

Map<Integer, List<Integer>> map