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:
  1. By using Stream concat() method 
  2. By using addAll() method
  3. 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]

More related topics,

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String