In Java 8:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream())
.collect(Collectors.toList());
Java 16+:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()).toList();
Answer from Dale Emery on Stack Overflow Top answer 1 of 16
1155
In Java 8:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream())
.collect(Collectors.toList());
Java 16+:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()).toList();
2 of 16
702
Off the top of my head, I can shorten it by one line:
List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
Java 8 Stream API Interview Questions: Merge Two Lists using ...
08:20
Merge Two Sorted Lists - 21. LeetCode - Java - YouTube
12:40
How to Join Two Lists in Java - YouTube
01:50
Efficiently Merge Two Lists in Java: A Guide to Combining Names ...
10:23
Merge Two List into a Single List Using Stream API | Learn with Mr.
04:03
LeetCode Merge Two Sorted Lists Solution Explained - Java
Scaler
scaler.com โบ home โบ topics โบ java program to merge two lists
Java Program to Merge Two Lists | Scaler Topics
June 9, 2024 - We can merge two lists in Java using the union() method. It returns a new concatenated list after receiving two list arguments.
Educative
educative.io โบ answers โบ how-to-merge-two-lists-in-java
How to merge two lists in Java
In this shot, we have seen how to merge two lists in Java by using the addAll() method and Java Stream.
Programiz
programiz.com โบ java-programming โบ examples โบ merge-two-lists
Java Program to Merge two lists
import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { // create first list List<Integer> prime = new ArrayList<>(); prime.add(2); prime.add(3); prime.add(5); System.out.println("First List: " + prime); // create second list List<Integer> even = new ArrayList<>(); even.add(4); even.add(6); System.out.println("Second List: " + even); // create merged list List<Integer> numbers = new ArrayList<>(); numbers.addAll(prime); numbers.addAll(even); System.out.println("Merged List: " + numbers); } }
Baeldung
baeldung.com โบ home โบ java โบ java collections โบ java map โบ combining two lists into a map in java
Combining Two Lists Into a Map in Java | Baeldung
June 27, 2025 - We then iterate through each element in KEY_LIST using a for loop, and for each element, we retrieve the corresponding element from VALUE_LIST using the same index i. Then, the put() method fills the key-value pair into the result map. Stream API provides many concise and efficient ways to manipulate Java collections. So next, letโs use the Java Stream API to associate two lists:
Vultr Docs
docs.vultr.com โบ java โบ examples โบ merge-two-lists
Java Program to Merge two lists | Vultr Docs
December 16, 2024 - This code initially creates two lists, list1 and list2, and then merges them into mergedList. The addAll() method from the ArrayList class is used to add all elements from list2 to mergedList which already contains the elements from list1. The Stream API, introduced in Java 8, offers a more functional approach to handle collections.
TutorialsPoint
tutorialspoint.com โบ java-program-to-concatenate-two-list
Java program to combine two list by alternatively taking elements
May 31, 2024 - The Java program to combine two lists by alternatively taking elements is a simple code snippet that takes in two lists of any type of object that can be stored in a list and returns a new list that contains elements from both input lists alternately.
Programiz
programiz.com โบ java-programming โบ examples โบ join-two-lists
Java Program to Join Two Lists
In the above program, we used union() method to join the given lists to joined. import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class JoinLists { public static void main(String[] args) { List<String> list1 = new ...
Codemia
codemia.io โบ home โบ knowledge hub โบ how do i join two lists in java?
How do I join two lists in Java? | Codemia
January 2, 2025 - The addAll() method is part of the List interface and is used to append all elements from one list to another. It ensures that the elements are added in the order they appear in the source list.
Techie Delight
techiedelight.com โบ home โบ java โบ join two lists in java
Join two lists in Java | Techie Delight
July 7, 2026 - Here weโre obtaining a stream consisting of elements from both the list using the static factory method Stream.of() and accumulating all elements into a new list using a Collector. ... We can avoid using a collector by accumulating all elements using forEach() instead, as shown below: ... The Java Stream provides concat() that takes two streams as input and creates a lazily concatenated stream whose elements are all the elements of the first stream, followed by all the elements of the second stream.
Coderanch
coderanch.com โบ t โบ 696394 โบ java โบ Combining-lists-Java
Combining lists using Java 8 (Features new in Java 8 forum at Coderanch)
July 9, 2018 - Piet Souris wrote:Indeed, and I don't think that ListZip class is simple. For instance So, in twoLists we have that A is List and B is List. However, the zip method takes a List<A> and a List<B> as parameters. Very confusing. But take note that the generic A and B from ListZip are not the same as the generic A and B from the zip method!
GeeksforGeeks
geeksforgeeks.org โบ java โบ java-program-to-combine-two-list-by-alternatively-taking-elements
Java Program to Combine Two List by Alternatively Taking Elements - GeeksforGeeks
August 8, 2022 - List1 contents: Geeks Geeks portal List2 contents: for is CSE portal Merged List contents: Geeks for Geeks is CSE portal portal ... // Java Program to Combine Two List // by Alternatingly Taking Elements // importing required packages import java.io.*; import java.util.*; import java.util.Iterator; // Class class GFG { // Main driver method public static void main(String[] args) { // Creating(declaring) List1 List<Integer> list1 = new ArrayList<Integer>(); // Adding elements to List1 // Custom inputs list1.add(2); list1.add(4); list1.add(6); // Creating(declaring) List2 List<Integer> list2 = n
Top answer 1 of 7
178
Java 8 has an easy way of doing it with the help of Stream API shown in the code below. We have basically created a stream with all the lists , and then as we need the individual contents of the lists, there is a need to flatten it with flatMap and finally collect the elements in a List.
List<Integer>list1=Arrays.asList(1,2,3);
List<Integer>list2=Arrays.asList(4,5,6);
List<Integer>list3=Arrays.asList(7,8,9);
List<Integer>list4=Arrays.asList(10,0,-1);
List<Integer> newList = Stream.of(list1, list2, list3,list4)
.flatMap(Collection::stream)
.collect(Collectors.toList());
System.out.println(newList); // prints [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, -1]
2 of 7
28
Adding other alternatives:
OPTION 1:
List<Integer> joinedList = joinLists(list1, list2, list3, list4);
public static <T> List<T> joinLists(List<T>... lists) {
return Arrays.stream(lists).flatMap(Collection::stream).collect(Collectors.toList());
}
OPTION 2:
List<Integer> joinedList = new ArrayList<>();
Stream.of(list1, list2, list3, list4).forEach(joinedList::addAll);