How to join two lists in Java?
In this section, we will show you how to join two lists in Java.
Following ways can be used to join two lists in Java:
- By using Stream concat() method
- By using addAll() method
- By using Apache Commons Collections ListUtils.union() method
Example 1: Join Two Lists using Stream concat() method
Stream.concat() method creates a concatenated stream in which the 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.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
// Driver code
public static void main(String[] args)
{
List<String> list1 =
new ArrayList<String>(Arrays.
asList("Java", "Kotlin","Python"));
List<String> list2 =
new ArrayList<String>(Arrays.
asList("Go", "C","Ruby"));
List<String> result = Stream.
concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
System.out.println(result);
}
}
Console Output:
[Java, Kotlin, Python, Go, C, Ruby]
Example 2: Java 16+ example
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Main {
// Driver code
public static void main(String[] args)
{
List<String> list1 =
new ArrayList<String>(Arrays.
asList("Java", "Kotlin","Python"));
List<String> list2 =
new ArrayList<String>(Arrays.
asList("Go", "C","Ruby"));
List<String> result = Stream.
concat(list1.stream(), list2.stream()).toList();
System.out.println(result);
}
}
Console Output:
[Java, Kotlin, Python, Go, C, Ruby]
Example 3: Join Two Lists using addAll()
addAll () method appends the entire given collection to the end of the list.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
// Driver code
public static void main(String[] args)
{
List<String> list1 =
new ArrayList<String>(Arrays.
asList("Java", "Kotlin","Python"));
List<String> list2 =
new ArrayList<String>(Arrays.
asList("Go", "C","Ruby"));
List<String> result = new ArrayList<>();
result.addAll(list1);
result.addAll(list2);
System.out.println(result);
}
}
Console Output:
[Java, Kotlin, Python, Go, C, Ruby]
Example 4: Join Two Lists using Apache Commons Collections union() method
Download Apache Commons Collections
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
The ListUtils.union() method accepts 2 List arguments and returns a new list containing the second list appended to the first list.
import org.apache.commons.collections.ListUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
// Driver code
public static void main(String[] args)
{
List<String> list1 =
new ArrayList<String>(Arrays.
asList("Java", "Kotlin","Python"));
List<String> list2 =
new ArrayList<String>(Arrays.
asList("Go", "C","Ruby"));
List<String> result = ListUtils.union(list1, list2);
System.out.println(result);
}
}
Console Output:
[Java, Kotlin, Python, Go, C, Ruby]