Java 8 Streams - 对元组流进行分组

时间:2015-09-20 14:11:01

标签: java java-8 java-stream

编辑::我正在改写这个问题,以便更清晰

我写了这段代码

    List<ImmutablePair<Integer, Integer>> list = new ArrayList<ImmutablePair<Integer, Integer>>();
    list.add(new ImmutablePair(1, 1));
    list.add(new ImmutablePair(1, 1));
    list.add(new ImmutablePair(1, 1));
    list.add(new ImmutablePair(2, 2));
    list.add(new ImmutablePair(2, 2));
    list.add(new ImmutablePair(2, 2));
    list.add(new ImmutablePair(3, 3));
    list.add(new ImmutablePair(3, 3));
    list.add(new ImmutablePair(3, 3));
    Stream<ImmutablePair<Integer, Integer>> stream = list.stream();
    Map<Integer, Integer> result = stream.collect(Collectors.groupingBy(
       ImmutablePair::getLeft,
            Collectors.mapping(
                    ImmutablePair::getRight,
                    Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
            )                
    ));

我想使用steams处理此列表,以便输出是包含键(1,3),(2,6),(3,9)的映射

所以基本上,我们将元组的左侧项目分组,然后对正确的项目求和。

此代码无法编译,编译器表示无法解析getLeft,getRight方法。

以下是编译器的错误消息

/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:229: error: method summingInt in class Collectors cannot be applied to given types;
                            Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
                                      ^
  required: ToIntFunction<? super T#1>
  found: Comparator<Object>
  reason: no instance(s) of type variable(s) T#2,U exist so that Comparator<T#2> conforms to ToIntFunction<? super T#1>
  where T#1,T#2,U are type-variables:
    T#1 extends Object declared in method <T#1>summingInt(ToIntFunction<? super T#1>)
    T#2 extends Object declared in method <T#2,U>comparing(Function<? super T#2,? extends U>)
    U extends Comparable<? super U> declared in method <T#2,U>comparing(Function<? super T#2,? extends U>)
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:229: error: incompatible types: cannot infer type-variable(s) T,U
                            Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
                                                                      ^
    (argument mismatch; invalid method reference
      no suitable method found for getRight(Object)
          method Pair.getRight() is not applicable
            (actual and formal argument lists differ in length)
          method ImmutablePair.getRight() is not applicable
            (actual and formal argument lists differ in length))
  where T,U are type-variables:
    T extends Object declared in method <T,U>comparing(Function<? super T,? extends U>)
    U extends Comparable<? super U> declared in method <T,U>comparing(Function<? super T,? extends U>)
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:229: error: invalid method reference
                            Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
                                                                       ^
  non-static method getRight() cannot be referenced from a static context
  where R is a type-variable:
    R extends Object declared in class ImmutablePair
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:228: error: invalid method reference
                            ImmutablePair::getRight,
                            ^
  non-static method getRight() cannot be referenced from a static context
  where R is a type-variable:
    R extends Object declared in class ImmutablePair
/Users/abhi/JavaProjects/MovieLambda2/src/main/java/com/abhi/MovieLambda2.java:226: error: invalid method reference
                    ImmutablePair::getLeft,
                    ^
  non-static method getLeft() cannot be referenced from a static context
  where L is a type-variable:
    L extends Object declared in class ImmutablePair

0 个答案:

没有答案