How to Join Two ArrayList

JavaJavaBeginner
Practice Now

Introduction

In Java, ArrayList is an implementation class of the List interface in the collection framework and is used to store data. Joining two ArrayLists 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.

Creating ArrayLists

First, let's create two ArrayLists that we want to join. Here's an example where we create two ArrayLists of integers:

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);

        ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1060,1070));
		System.out.println(arrList2);
    }
}

To run this code, type the following command in the terminal:

javac Main.java && java Main

This will output the following:

[1030, 1020, 1010, 1040]
[1050, 1060, 1070]

Joining ArrayLists with addAll()

To join two ArrayLists using the addAll() method, follow the code below:

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);

        ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1060,1070));
		System.out.println(arrList2);

		// Joining two ArrayList
		arrList1.addAll(arrList2);
		System.out.println("After Joining:");
		System.out.println(arrList1);
    }
}

To run this code, type the following command in the terminal:

javac Main.java && java Main

This will output the following:

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

Joining ArrayLists with flatMap()

We can also join two ArrayLists by using the flatMap() method and distinct() method by following the code 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);

		ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1030,1070));
		System.out.println(arrList2);

		// Joining two ArrayList
		List<Integer> arrList3 = Stream.of(arrList1, arrList2)
									.flatMap(Collection::stream)
									.distinct()
									.collect(Collectors.toList());
		System.out.println("After Joining:");
		System.out.println(arrList3);
	}
}

To run this code, type the following command in the terminal:

javac Main.java && java Main

This will output the following:

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

Complete Code

Here's the complete code to join two ArrayLists in Java:

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);

		ArrayList<Integer> arrList2 = new ArrayList<>(Arrays.asList(1050,1030,1070));
		System.out.println(arrList2);

		// Joining two ArrayList
		List<Integer> arrList3 = Stream.of(arrList1, arrList2)
									.flatMap(Collection::stream)
									.distinct()
									.collect(Collectors.toList());
		System.out.println("After Joining:");
		System.out.println(arrList3);
	}
}

Running the Code

To run the code, type the following command in the terminal:

javac Main.java && java Main

This will output the following:

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

Summary

In this lab, we have shown you how to join two ArrayLists in Java using the addAll() method of List and flatMap() method of stream API. We have also shown you an example of joining unique elements from two different ArrayLists. We hope this lab was helpful to you!

Other Java Tutorials you may like