BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Copy Elements of One ArrayList to Another ArrayList in Java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial, we will write a java program to copy elements of one ArrayList to another ArrayList in Java. We will be using addAll() method of ArrayList class to do that.

public boolean addAll(Collection c)

When we call this method like this:

newList.addAll(oldList);

It appends all the elements of oldList to the newList. It throws NullPointerException, if oldList is null.
Note: This method doesn’t clone the ArrayList, instead it appends the elements of one ArrayList to another ArrayList.

Copy elements from one ArrayList to another ArrayList

In the following example, we have an ArrayList al that contains three elements. We have another List list that contains some elements, we are copying the elements of list to the al. This is done using addAll() method, which copies the elements of one List to another another list. This operation doesn’t remove the existing elements of the list, in which the elements are copied.

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

public class ListToArrayListExample {

   public static void main(String a[]){
      ArrayList<String> al = new ArrayList<String>();

      //Adding elements to the ArrayList
      al.add("Text 1");
      al.add("Text 2");
      al.add("Text 3");

      System.out.println("ArrayList Elements are: "+al);

      //Adding elements to a List
      List<String> list = new ArrayList<String>();
      list.add("Text 4");
      list.add("Text 5");
      list.add("Text 6");

      //Adding all elements of list to ArrayList using addAll
      al.addAll(list);
      System.out.println("Updated ArrayList Elements: "+al);
   }
}

Output:

ArrayList Elements are: [Text 1, Text 2, Text 3]
Updated ArrayList Elements: [Text 1, Text 2, Text 3, Text 4, Text 5, Text 6]

Recommended Articles:

  • Swap two elements in an ArrayList
  • Synchronize ArrayList in Java
  • Override toString() method for ArrayList
  • Compare two ArrayList in Java
❮ Java Collections

Top Related Articles:

  1. Convert ArrayList to Array in Java
  2. How to get the last element of Arraylist?
  3. How to Convert an array to ArrayList in java
  4. How to loop HashMap in java
  5. Vector in Java

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. himanhsu sharma says

    August 12, 2016 at 10:22 AM

    addAll() method is not throwing nullpointerException.if the list is empty

    Reply
  2. Anvesh says

    November 4, 2016 at 7:20 AM

    @ Himanshu,

    ArrayList never be a null, instead it is an empty when u create. if you want more clarity, execute the below code.

    List list = new ArrayList();
    List newList = new ArrayList();
    list.addAll(newList);
    System.out.println(list); // this works fine and creates a list with empty

    List list = null;
    List newList = null;
    list.addAll(newList);
    System.out.println(list); // this will give you NPE. since List is not initiated with any of its implemented class

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap