- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
编辑::我正在改写问题以便更清楚
这段代码是我写的
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))
)
));
我想使用 steam 处理这个列表,以便输出是一个包含键 (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
最佳答案
假设您的 ImmutablePair 类如下所示(此类可能需要是静态的,具体取决于示例代码是否从 main 方法中执行):
class ImmutablePair<T,T2> {
private final T left;
private final T2 right;
public ImmutablePair(T left, T2 right) {
this.left = left;
this.right = right;
}
public T getLeft() {
return left;
}
public T2 getRight() {
return right;
}
}
这似乎对我有用:
List<ImmutablePair<Integer, Integer>> list = new ArrayList<>();
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));
Map<Integer, Integer> collect = list.stream()
.collect(
Collectors.groupingBy(
ImmutablePair::getLeft,
Collectors.summingInt(ImmutablePair::getRight)));
System.out.println(collect);
结果是:
{1=3, 2=6, 3=9}
可能的问题
问题出在这段代码中:
Collectors.mapping(
ImmutablePair::getRight,
Collectors.summingInt(Comparator.comparing(ImmutablePair::getRight))
)
Collectors.mapping 收集器首先将类型 T 的对象适配为类型 U 的对象,然后在类型 U 上运行收集器。( That's the gist of the javadoc for me )
所以你正在做的是这样的:
Collectors.mapping(ImmutablePair::getRight, Collectors.summingInt(Integer::valueOf))
但您不需要这个,因为您可以通过直接调用 Collectors.summingInt 来简化它:
Collectors.summingInt(ImmutablePair::getRight)
关于Java 8 Streams - 对元组流进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680310/
我通过它们的5 元组(src IP、dst 端口、sport、dport、传输协议(protocol)编号)识别互联网流量,我想将这个 5-元组为更紧凑的字母数字 ID,供我的脚本内部使用。 我在 P
我是一名优秀的程序员,十分优秀!