How to join two Lists in Java

In this article, we show you 2 examples to join two lists in Java.

  1. JDK – List.addAll()
  2. Apache Common – ListUtils.union()

1. List.addAll() example

Just combine two lists with List.addAll().

JoinListsExample.java

package com.mkyong.example;

import java.util.ArrayList;
import java.util.List;

public class JoinListsExample {

	public static void main(String[] args) {
	
		List<String> listA = new ArrayList<String>();
		listA.add("A");
		
		List<String> listB = new ArrayList<String>();
		listB.add("B");
		
		List<String> listFinal = new ArrayList<String>();
		listFinal.addAll(listA);
		listFinal.addAll(listB);
		
		//same result
		//List<String> listFinal = new ArrayList<String>(listA);
		//listFinal.addAll(listB);
		
		System.out.println("listA : " + listA);
		System.out.println("listB : " + listB);
		System.out.println("listFinal : " + listFinal);
		
	}

}

Output


listA : [A]
listB : [B]
listFinal : [A, B]
Append Lists
To append ListB to the end of ListA, uses


listA.addAll(listB);

2. ListUtils.union example

Apache common library – ListUtils.union().

JoinListsExample2.java

package com.mkyong.example;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.ListUtils;

public class JoinListsExample2 {

	public static void main(String[] args) {
	
		List<String> listA = new ArrayList<String>();
		listA.add("A");
		
		List<String> listB = new ArrayList<String>();
		listB.add("B");

		List<String> listFinal = ListUtils.union(listA, listB);
		
		System.out.println("listA : " + listA);
		System.out.println("listB : " + listB);
		System.out.println("listFinal : " + listFinal);
		
	}

}

Output


listA : [A]
listB : [B]
listFinal : [A, B]

Dig into the source code, the ListUtils.union is using the same List.addAll() to combine lists.

ListUtils.java

    public static List union(final List list1, final List list2) {
        final ArrayList result = new ArrayList(list1);
        result.addAll(list2);
        return result;
    }

References

  1. How To Count Duplicated Items In Java List
  2. Apache common ListUtils JavaDoc
  3. List.addAll() JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
0 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
9 Comment authors
hilmidharmarajNirav PrajapatiJoão Paulomrrdev Recent comment authors
newest oldest most voted
mrrdev
Guest
mrrdev

Very clear explanation of appending one list to another. This is a great Java resource.

hilmi
Guest
hilmi

What if i need to join to personel lists in a situation like below?
-List A has personels address information (Same Personel class, other information are null except id)
-List B has personels work information (Same Personel class, other information are null except id)
-Both came from different tables, no option to use sql to fetch both to one list at initialization.
-Both has the same personels; e.g. john, jack, rose and julie.

Goal: Fill “List A”‘s lacking information from “List B”.

Nirav Prajapati
Guest
Nirav Prajapati

Nice Blog

João Paulo
Guest
João Paulo

What about joining 3 lists or more?

alewiwi
Guest
alewiwi

I think the Union here is misleading because if we add a new item for example “D” to both list, the result should be listFinal[A,D,B] but the result will show two “D” ‘s listFinal[A,D,B,D] which contradict the union principal in math

johnsaida
Guest
johnsaida

plz upload the EJB3.0 Tutorials

yuda
Guest
yuda

nice i sucess for combine list with this method.

jhon grhesam
Guest
jhon grhesam

nice

dharmaraj
Guest
dharmaraj

sper one..