Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I append two Streams in Java 8?

Suppose I have two streams of the same type. Is it possible to append one stream to the other without converting them to lists beforehand?

Example:

Stream<MyClass> ms = ...;
Stream<MyClass> ns = ...;
return ms.append(ns);
like image 664
Bastian Avatar asked May 08 '14 15:05

Bastian


1 Answers

Yes.

Use Stream.concat(stream1, stream2), this will create a stream consisting of first the elements of stream1 and then the elements of stream2, if you want to maintain ordering. Also note that all applied predicates, etc. still work on a per-stream basis, they do not automagically hold for the concatenation of the two streams.

like image 154
skiwi Avatar answered Sep 18 '22 23:09

skiwi