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 copy and add all list elements to ArrayList in Java

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we will see how to copy and add all the elements of a list to ArrayList. In order to do that we will be using addAll method of ArrayList class.

public boolean addAll(Collection c)

It adds all the elements of specified Collection c to the end of the calling list. It throws NullPointerException if the specified Collection is null.

Complete Example of Copying List elements to ArrayList

package beginnersbook.com;
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 lements 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]

Enjoyed this post? Try these related posts

  1. Hashtable Iterator example – Java
  2. How to serialize HashMap in java
  3. Hashtable in java with example
  4. Deque Interface in Java Collections
  5. How to convert LinkedList to array using toArray() in Java
  6. Java – Convert Vector to List example

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 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