Merge two ArrayList in Java - Java code Examples

Overview

In this tutorial, we show you how to join two ArrayList in Java. There are three Ways to join two ArrayList. Using the addAll() method of collections, for loop with an ArrayList and Apache Commons Collections library.

Merge two ArrayList in Java

Join two ArrayList using ArrayList.addAll() method

package com.jackrutorial;

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

public class JoinArrayListExample1 {

 public static void main(String[] args) {
  List springData = new ArrayList();
  springData.add("Spring Data Aerospike");
  springData.add("Spring Data ArangoDB");
  springData.add("Spring Data Couchbase");
  
  List springSecurity = new ArrayList();
  springSecurity.add("Spring Security Kerberos");
  springSecurity.add("Spring Security OAuth");
  springSecurity.add("Spring Security SAML");
  
  List springProject = new ArrayList();
  springProject.addAll(springData);
  springProject.addAll(springSecurity);
  
  System.out.println("List Spring Project: " + springProject);
 }
}

Output

List Spring Project: [Spring Data Aerospike, Spring Data ArangoDB, Spring Data Couchbase, Spring Security Kerberos, Spring Security OAuth, Spring Security SAML]

Join two ArrayList using for loop with an ArrayList

package com.jackrutorial;

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

public class JoinArrayListExample2 {

 public static void main(String[] args) {
  List springData = new ArrayList();
  springData.add("Spring Data Aerospike");
  springData.add("Spring Data ArangoDB");
  springData.add("Spring Data Couchbase");
  
  List springSecurity = new ArrayList();
  springSecurity.add("Spring Security Kerberos");
  springSecurity.add("Spring Security OAuth");
  springSecurity.add("Spring Security SAML");
  
  List springProject = new ArrayList();
  for(String tmp : springData) {
   springProject.add(tmp);
  }
  
  for(String tmp : springSecurity) {
   springProject.add(tmp);
  }
  
  System.out.println("List Spring Project: " + springProject);
 }
}

Output

List Spring Project: [Spring Data Aerospike, Spring Data ArangoDB, Spring Data Couchbase, Spring Security Kerberos, Spring Security OAuth, Spring Security SAML]

Join two ArrayList with Apache Commons Collections library

We use the Apache commons-collections library (ex: commons-collections-3.0.jar) with ListUtils.union() method to joining two lists into a single ArrayList.
package com.jackrutorial;

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

public class JoinArrayListExample3 {

 public static void main(String[] args) {
  List springData = new ArrayList();
  springData.add("Spring Data Aerospike");
  springData.add("Spring Data ArangoDB");
  springData.add("Spring Data Couchbase");
  
  List springSecurity = new ArrayList();
  springSecurity.add("Spring Security Kerberos");
  springSecurity.add("Spring Security OAuth");
  springSecurity.add("Spring Security SAML");
  
  List springProject = ListUtils.union(springData, springSecurity);
  
  System.out.println("List Spring Project: " + springProject);
 }
}

Output

List Spring Project: [Spring Data Aerospike, Spring Data ArangoDB, Spring Data Couchbase, Spring Security Kerberos, Spring Security OAuth, Spring Security SAML]
Previous Post
Next Post

post written by: