Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

How to Join and Merge Two ArrayLists in Java - Example Tutorial

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 implementations 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 like 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 join the Complete Java Masterclass on Udemy, a comprehensive course of Java with lots of practice questions, quizzes, and diagrams.

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

Here is a simple Java program to demonstrate how to use the addAll() method of Collection interface to join elements of two array lists in Java. In this program, we have two ArrayList objects, first contains some UK-based banks like Barclays, Standard Chartered, and HSBC.

while the second list contains some US-based banks like Citigroup, Chase, Wells Fargo, and Bank of America. We finally create an ArrayList of global banks by joining the US and UK-based banks together using the addAll() method.

The above example is a really good scenario of when you should join ArrayList or any other type of List implementation like creating a big list from two smaller lists. If you want to learn more about the ArrayList class and its usages in Java applications, see these free Java online courses to learn more about ArrayList in Java. 

how to merge two list in Java

Joining Multiple 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 or merge two array lists in Java. You can use this technique to join not only lists but also any collection because the 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)
Thanks for reading this article so far. If you find this example of merging two list in Java using Java Collection Framework API then please share with your friends and colleagues. If you have any questions or feedback then please drop a note. 

1 comment:

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