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

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments
License