How to join two ArrayList in Java - Example

You can use the addAll() method from java.util.Collection interface to join two ArrayLists in Java. Since ArrayList implements List interface which actually extends the Collection interface, this method is available to all List implementation including ArrayList e.g. Vector, LinkedList. The Collection.addAll(Collection src) method takes a collection and adds all elements from it to the collection which calls this method e.g. target.addAll(source). After this call, the target will have all elements from both source and target ArrayList, which is like joining two ArrayList in Java. The second ArrayList will remain as it is but the first ArrayList on which you have added elements will have more elements. Its size will be equal to the sum of the size of first and second ArrayList.

You should remember, that with Generics in place you cannot join two different types of ArrayList e.g. you cannot join an Integer ArrayList to String ArrayList or vice-versa. If you want to create a heterogeneous ArrayList, then you should create two ArrayList of objects and join them together as shown below:

List<Object> list1 = new ArrayList<Object>();
List<Object> list2 = new ArrayList<Object>();

list1.addAll(list2); // now list1 has element of both list 1 and list 2 

This ArrayList can contain any type of object e.g. Integer, String, Float, and Double. You can also read Big Java: Early Objects 5th Edition by Cay S. Horstmann, a comprehensive guide of Java with lots of practice questions, quizzes, and diagrams.

Java Program to join ArrayList - ArrayList.addAll() Example

Here is a simple Java program to demonstrate how to use addAll() method of Collection interface to join elements of two array list in Java. In this program, we have two ArrayList objects, first contains some UK based banks e.g. Barclays, Standard Chartered, and HSBC, while the second list contains some US based banks e.g. Citigroup, Chase, Wells Fargo, and Bank of America. We finally create an ArrayList of global banks by joining US and UK based bank together using addAll() method.

The above example is a really good scenario of when you should join ArrayList or any other type of List implementation e.g. to create a big list from two smaller lists. If you want to learn more about ArrayList class and its usages in Java application, see Big Java: Early Objects by Cay S. Horstmann , one of the most comprehensive guides of Java programming language.

How to combine two arraylist in Java

Joining ArrayList in Java using Collection.addAll() 
import java.util.ArrayList;
import java.util.List;

 * Java Program to join two ArrayLists into one 

public class ArrayListJoiner {

  public static void main(String[] args) {

    // first ArrayList
    List<String> UKBasedBanks = new ArrayList<>();
    UKBasedBanks.add("Standard Charated");

    // second ArrayList
    List<String> USABanks = new ArrayList<>();
    USABanks.add("Bank of America");
    USABanks.add("Wells Fargo");

    System.out.println("first arraylist before joining : ");
    System.out.println("second arraylist before joining : ");

    // Joining two ArrayList
    // adding all elements of USABanks list to
    // UKBasedBanks

    System.out.println("first arraylist after joining : ");
    System.out.println("second arraylist after joining : ");



first ArrayList before joining : 
[Standard Charted, HSBC, Barclays]
second ArrayList before joining : 
[Citibank, Chase, Bank of America, Wells Fargo]
first ArrayList after joining : 
[Standard Charted, HSBC, Barclays, Citibank, Chase, Bank of America, Wells Fargo]
second ArrayList after joining : 
[Citibank, Chase, Bank of America, Wells Fargo]

That's all about how to join two array lists in Java. You can use this technique to join not only lists but also any collection because addAll() method is defined on the Collection interface it's available to list, set, and queue. Just remember that the source ArrayList will remain intact but target ArrayList will be modified to include elements from source ArrayList.

Related Java ArrayList Tutorials for Programmers
  • How to traverse an ArrayList in Java? (example)
  • How to sort an ArrayList in Java? (example)
  • How to reverse an ArrayList in Java? (example)
  • How to synchronize an ArrayList in Java? (example)
  • How to remove objects from ArrayList in Java? (solution)
  • How to remove duplicates from ArrayList in Java? (solution)
  • How to make an ArrayList read-only in Java? (solution)
  • How to get the first and last element of ArrayList in Java? (example)
  • How to create and initialize an ArrayList in the same line? (answer)

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java

1 comment:

Feel free to comment, ask questions if you have any doubt.