将两个Java 8流或一个额外元素添加到流中 [英] Adding two Java 8 streams, or an extra element to a stream

查看:148
本文介绍了将两个Java 8流或一个额外元素添加到流中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以添加流或额外元素,如下所示:

I can add streams or extra elements, like this:

Stream stream = Stream.concat(stream1, Stream.concat(stream2, Stream.of(element));

我可以随时添加新内容,像这样:

And I can add new stuff as I go, like this:

Stream stream = Stream.concat(
                       Stream.concat(
                              stream1.filter(x -> x!=0), stream2)
                              .filter(x -> x!=1),
                                  Stream.of(element))
                                  .filter(x -> x!=2);

但这很难看,因为 concat 是静态的。如果 concat 是一个实例方法,上面的例子将更容易阅读:

But this is ugly, because concat is static. If concat were an instance method, the above examples would be much easier to read:

 Stream stream = stream1.concat(stream2).concat(element);

 Stream stream = stream1
                 .filter(x -> x!=0)
                 .concat(stream2)
                 .filter(x -> x!=1)
                 .concat(element)
                 .filter(x -> x!=2);

我的问题是:

1) concat 是静态的有什么好的理由吗?或者是否有一些我缺少的等效实例方法?

1) Is there any good reason why concat is static? Or is there some equivalent instance method I'm missing?

2)无论如何,有没有更好的方法呢?

2) In any case, is there a better way of doing this?

推荐答案

如果为 Stream.concat Stream.of 静态导入 >,第一个例子可以写成如下:

If you add static imports for Stream.concat and Stream.of, the first example could be written as follows:

Stream<Foo> stream = concat(stream1, concat(stream2, of(element)));

使用通用名称导入静态方法会导致代码变得困难阅读和维护(名称空间污染)。因此,使用更有意义的名称创建自己的静态方法可能更好。但是,为了演示,我将坚持使用此名称。

Importing static methods with generic names can result in code that becomes difficult to read and maintain (namespace pollution). So, it might be better to create your own static methods with more meaningful names. However, for demonstration I will stick with this name.

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));
}

使用这两种静态方法(可选择与静态导入结合使用),两种示例可以写成如下:

With these two static methods (optionally in combination with static imports), the two examples could be written as follows:

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);

现在代码明显缩短了。但是,我同意可读性没有改善。所以我有另一个解决方案。

The code is now significantly shorter. However, I agree that the readability hasn't improved. So I have another solution.

在很多情况下,收集器可用于扩展流的功能。底部有两个收集器,这两个例子可以写成如下:

In a lot of situations, Collectors can be used to extend the functionality of streams. With the two Collectors at the bottom, the two examples could be written as follows:

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);

您所需语法与上述语法之间的唯一区别是,您必须替换 concat(...) collect(concat(...))。两个静态方法可以如下实现(可选择与静态导入结合使用):

The only difference between your desired syntax and the syntax above is, that you have to replace concat(...) with collect(concat(...)). The two static methods can be implemented as follows (optionally used in combination with static imports):

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 。这两种操作都会对程序的行为产生重大影响。但是,如果可读性性能更重要,那么它可能仍然是一种非常有用的方法。


Of course there is a drawback with this solution that should be mentioned. collect is a final operation that consumes all elements of the stream. On top of that, the collector concat creates an intermediate ArrayList each time it is used in the chain. Both operations can have a significant impact on the behaviour of your program. However, if readability is more important than performance, it might still be a very helpful approach.

这篇关于将两个Java 8流或一个额外元素添加到流中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆