Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

How to Join Two ArrayList in Java

In this post, we are going to join two ArrayList in Java. ArrayList is an implementation class of List interface in the collection framework and used to store data.

Joining two ArrayList is actually a process of combining two ArrayList elements into a single ArrayList. It is helpful when we have multiple data streams and want to collect them into a single stream.

To Join ArrayList we used the addAll() method of List and flatMap() method of stream API.

The addAll() method adds is used to add elements of one ArrayList to another and takes a single argument. Let's see the examples.

Time for an Example:

Let's create an example to join two ArrayList. Here we are using addAll() method and passed arrList2 to as an argument and see we get all the elements into a single ArrayList.

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
	public static void main(String[] args){
		ArrayList<Integer> arrList1 = new ArrayList<>();
		arrList1.add(1030);
		arrList1.add(1020);
		arrList1.add(1010);
		arrList1.add(1040);
		System.out.println(arrList1);
		// Join two ArrayList
		ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1060,1070));
		System.out.println(arrList2);
		arrList1.addAll(arrList2);
		System.out.println("After Joining:");
		System.out.println(arrList1);
		
	}
}


[1030, 1020, 1010, 1040]
[1050, 1060, 1070]
After Joining:
[1030, 1020, 1010, 1040, 1050, 1060, 1070]

Example:

You can use Java stream also to combine two ArrayList elements. Here, we are using flatMap() method of stream that collect all the elements into single stream and returns a list.

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 {
	public static void main(String[] args){
		ArrayList<Integer> arrList1 = new ArrayList<>();
		arrList1.add(1030);
		arrList1.add(1020);
		arrList1.add(1010);
		arrList1.add(1040);
		System.out.println(arrList1);
		// Join two ArrayList
		ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1060,1070));
		System.out.println(arrList2);
        List<Integer> arrList3 = Stream.of(arrList1, arrList2)
                .flatMap(x -> x.stream())
                .collect(Collectors.toList());

		System.out.println("After Joining:");
		System.out.println(arrList3);
		
	}
}


[1030, 1020, 1010, 1040]
[1050, 1060, 1070]
After Joining:
[1030, 1020, 1010, 1040, 1050, 1060, 1070]

Example: Join Unique Elements

If you want to collect only unique elements from two different ArrayLists then you can use distinct() method with flatMap() that returns a list of unique elements. See the example below.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
	public static void main(String[] args){
		ArrayList<Integer> arrList1 = new ArrayList<>();
		arrList1.add(1030);
		arrList1.add(1020);
		arrList1.add(1010);
		arrList1.add(1040);
		System.out.println(arrList1);
		// Join two ArrayList
		ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1030,1070));
		System.out.println(arrList2);
		List<Integer> arrList3 = Stream.of(arrList1, arrList2)
									.flatMap(Collection::stream)
									.distinct()
									.collect(Collectors.toList());
		System.out.println("After Joining:");
		System.out.println(arrList3);
		
	}
}


[1030, 1020, 1010, 1040]
[1050, 1030, 1070]
After Joining:
[1030, 1020, 1010, 1040, 1050, 1070]