如果为 Stream.concat 和 Stream.of 添加 静态导入 ,则第一个示例可以编写如下:
Stream<Foo> stream = concat(stream1, concat(stream2, of(element)));
导入具有通用名称的 静态方法 可能会导致代码变得难以阅读和维护( 命名空间污染 )。因此,最好使用更有意义的名称创建自己的 静态方法
。但是,为了演示起见,我将坚持使用该名称。
public static <T> Stream<T> concat(Stream<? extends T> lhs, Stream<? extends T> rhs) { return Stream.concat(lhs, rhs);}public static <T> Stream<T> concat(Stream<? extends T> lhs, T rhs) { return Stream.concat(lhs, Stream.of(rhs));}使用这两种静态方法(可以选择与静态导入结合使用),可以将两个示例编写如下:
Stream<Foo> stream = concat(stream1, concat(stream2, element));Stream<Foo> stream = concat( concat(stream1.filter(x -> x!=0), stream2).filter(x -> x!=1), element) .filter(x -> x!=2);
现在,该代码明显缩短了。但是,我同意可读性并没有提高。所以我有另一个解决方案。
在许多情况下,可以使用 收集器 来 扩展 流的功能。将两个 收集器 放在底部,可以将两个示例编写如下:
Stream<Foo> stream = stream1.collect(concat(stream2)).collect(concat(element));Stream<Foo> stream = stream1 .filter(x -> x!=0) .collect(concat(stream2)) .filter(x -> x!=1) .collect(concat(element)) .filter(x -> x!=2);
所需语法和上面语法之间的唯一区别是,您必须用 collect(concat(…)) 替换 concat(… )
。这两种静态方法可以按以下方式实现(可以选择与静态导入结合使用):
private static <T,A,R,S> Collector<T,?,S> combine(Collector<T,A,R> collector, Function<? super R, ? extends S> function) { return Collector.of( collector.supplier(), collector.accumulator(), collector.combiner(), collector.finisher().andThen(function));}public static <T> Collector<T,?,Stream<T>> concat(Stream<? extends T> other) { return combine(Collectors.toList(), list -> Stream.concat(list.stream(), other));}public static <T> Collector<T,?,Stream<T>> concat(T element) { return concat(Stream.of(element));}当然,这种解决方案有一个缺点,应该提到。 collect 是消耗流中所有元素的最终操作。最重要的是,每次在链中使用收集器 concat时,
都会创建一个中间 ArrayList 。两种操作都会对程序的行为产生重大影响。但是,如果 可读性 比 性能
更重要,它可能仍然是一个非常有用的方法。



