Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 922
skiwi Avatar asked Mar 30 '14 11:03

skiwi


People also ask

Does Java 8 support streams?

Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.

How do I concatenate 3 streams?

With three streams we could write Stream. concat(Stream. concat(a, b), c) .

Why is Java 8 stream lazy?

Streams are lazy because intermediate operations are not evaluated until terminal operation is invoked. Each intermediate operation creates a new stream, stores the provided operation/function and return the new stream. The pipeline accumulates these newly created streams.

What are the advantages of Java 8 streams?

There are a lot of benefits to using streams in Java, such as the ability to write functions at a more abstract level which can reduce code bugs, compact functions into fewer and more readable lines of code, and the ease they offer for parallelization.


1 Answers

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); 
like image 179
Boris the Spider Avatar answered Sep 24 '22 08:09

Boris the Spider