对于我的具体情况,我想简化使用功能组合。
例如:
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, Sets.SetView<Integer>> f = Sets::intersection;
Function<Sets.SetView<Integer>, ImmutableSet<Integer>> g = Sets.SetView::immutableCopy;
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> biFunction = f.andThen(g);
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce(biFunction)
.orElse(ImmutableSet.of());
获取编译错误:
argument mismatch BiFunction cannot be converted to BinaryOperator
相反,我需要这样做:
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce((a, b) -> Sets.intersection(a, b).immutableCopy())
.orElse(ImmutableSet.of());
但是这失去了合成提供的无意义样式。
我的问题是,为什么Stream API如此设计。 BinaryOperator是BiFunction,所以用超类型声明方法不是更有意义吗?
BinaryOperator<T> extends BiFunction<T,T,T>