beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to join/combine two ArrayLists in java

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we will see how to join (or Combine) two ArrayLists in Java. We will be using addAll() method to add both the ArrayLists in one final ArrayList.

Example:

In this example we are merging two ArrayLists in one single ArrayList and then displaying the elements of final List.

package beginnersbook.com;
import java.util.ArrayList;
public class Details
{
    public static void main(String [] args)
    {
        //First ArrayList
        ArrayList<String> arraylist1=new ArrayList<String>();
        arraylist1.add("AL1: E1");
        arraylist1.add("AL1: E2");
        arraylist1.add("AL1: E3");

        //Second ArrayList
        ArrayList<String> arraylist2=new ArrayList<String>();
        arraylist2.add("AL2: E1");
        arraylist2.add("AL2: E2");
        arraylist2.add("AL2: E3");

        //New ArrayList
        ArrayList<String> al= new ArrayList<String>();
        al.addAll(arraylist1);
        al.addAll(arraylist2);

        //Displaying elements of the joined ArrayList
        for(String temp: al){
            System.out.println(temp);
        }
    }
}

Output:

AL1: E1
AL1: E2
AL1: E3
AL2: E1
AL2: E2
AL2: E3

Enjoyed this post? Try these related posts

  1. How to convert LinkedList to array using toArray() in Java
  2. HashMap in Java with Example
  3. Remove Key-value mapping from TreeMap example
  4. Difference between HashMap and Hashtable
  5. Java – Remove element from a specific index in LinkedList example
  6. Java – Remove first and last element from LinkedList example

Comments

  1. Rajasekhar says

    February 25, 2016 at 4:59 AM

    Hai Thanks for your awesome tutorial i have one small doubt regarding this joining to Lists can we join String type and Integer type together if yes please give me one small example
    once again thanks for your work u made my learning easy

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap