Close

Java 8 Streams - Stream.concat Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.Stream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.StreamStreamLogicBig

Method:

static <T> Stream<T> concat(Stream<? extends T> a,
                            Stream<? extends T> b)

Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel. When the resulting stream is closed, the close handlers for both input streams are invoked.


Examples


package com.logicbig.example.stream;

import java.util.Arrays;
import java.util.stream.Stream;

public class ConcatExample {

public static void main(String... args) {
Stream<String> s1 = Arrays.asList("one", "two", "three").stream();
Stream<String> s2 = Arrays.asList("four", "five", "six").stream();

Stream<String> s3 = Stream.concat(s1, s2);
s3.forEach(System.out::println);
}
}

Output

one
two
three
four
five
six




See Also