Does Java 8 lack a Stream.concat working on a varags of streams?
Currently we have the following Stream.concat
in Java 8:
public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b);
I am surprised as to why there is no version taking a varargs of Stream<? extends T>
?
Currently I have code written like:
Stream<Integer> resultStream = Stream.concat(stream1, Stream.concat(stream2, Stream.of(element)))
.filter(x -> x != 0)
.filter(x -> x != 1)
.filter(x -> x != 2);
If a varargs of this signature were available:
public static <T> Stream<T> concat(Stream<? extends T>... streams);
Then I could write it much more clearly as:
Stream<Integer> resultStream = Stream.concat(
stream1,
stream2,
Stream.of(element)
)
.filter(x -> x != 0)
.filter(x -> x != 1)
.filter(x -> x != 2);
Without all kinds of nested Stream.concat
calls.
Or are there other reasons why it is not provided?
I cannot think of such reasons, as we end up doing the job of a varargs call anyway now.
Solution 1:
Just flatMap
it:
public static void main(final String[] args) throws Exception {
final Stream<String> stream1 = /*some stream*/
final Stream<String> stream2 = /*some stream*/
final Stream<String> stream3 = /*some stream*/
final Stream<String> stream4 = /*some stream*/
final Stream<String> stream5 = /*some stream*/
final Stream<String> stream = Stream.of(stream1, stream2, stream3, stream4, stream5).flatMap(Function.identity());
}
In your example:
Stream<Integer> resultStream = Stream.of(stream1, stream2, Stream.of(element))
.flatMap(identity())
.filter(x -> x != 0)
.filter(x -> x != 1)
.filter(x -> x != 2);
Solution 2:
Gleaned from a message in the thread linked by @RohitJain:
Stream.of(s1, s2, s3, ...)
/* .parallel() if you want*/
.reduce(Stream::concat)
.orElseGet(Stream::empty);
Solution 3:
In Google Guava v21.0+ there is:
com.google.common.collect.Streams#concat
method
Solution 4:
SO, aggregating answers of.flatMap() vs of.reduce().orElseGet() : of.flatMap
can't process infinite streams, when of.reduce()
- can. See the test example below:
@RunWith(JUnit4.class)
public class StreamConcatTest {
@Rule
public Timeout globalTimeout = Timeout.seconds(3);
private static final Random randomSupplier = new Random(System.currentTimeMillis());
private Stream<Stream<Integer>> mergedStream;
@Before
public void setUp() throws Exception {
Stream<Integer> infinite = Stream.concat(
Stream.of(1, 2, 3, 4, 5),
Stream.generate(randomSupplier::nextInt)
);
Stream<Integer> finite1 = Stream.of(100, 101, 102, 103);
Stream<Integer> finite2 = Stream.of(222, 333, 444, 555);
mergedStream = Stream.of(infinite, finite1, finite2);
}
@Test
public void of_flatMap_FAILS_BY_TIMEOUT() throws Exception {
Stream<Integer> streamToTest = mergedStream
.flatMap(i -> i);
assertThat(streamToTest
.skip(3)
.findFirst() // this should break infinite stream, but can't
.orElse(-1),
is(4));
}
@Test
public void of_reduce_SUCCESS() throws Exception {
Stream<Integer> streamToTest = mergedStream
.reduce(Stream::concat)
.orElseGet(Stream::empty);
assertThat(streamToTest
.skip(3)
.findFirst() // this really breaks infinite stream
.orElse(-1),
is(4));
}
}