Question

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);
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top