adam bien's blog

Adding / Merging / Joining two Collections / Lists 📎

To join to List / Collection instances, you can concat their streams:

import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
public class JoiningTwoLists {

    @Test
    public void join() {
        var first = List.of(1,2);
        var second = List.of(3,4);
        var joinedList = Stream.concat(first.stream(), second.stream()).toList();
        joinedList.forEach(System.out::println);
    }
}

Prints: 1 2 3 4

See it in action: