English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)/O)

Java Reader/Writer

Java Other Topics

Java ArrayList addAll() usage and example

Java ArrayList Methods

 The Java ArrayList addAll() method adds all elements of the collection to the ArrayList.

The syntax of the addAll() method is:

arraylist.addAll(int index, Collection c)

addAll() parameters

The ArrayList addAll() method can take two parameters:

  • index (Optional)- The index of all elements in the collection to be inserted

  • collection - The collection containing the elements to be inserted

If no parameter index is passed, the collection is added to the end of the ArrayList.

addAll() return value

  • If the collection has been successfully inserted into the ArrayList, it returns true.

  • If the specified collection is null, a NullPointerException is thrown.

  • If the index is out of range, an IndexOutOfBoundsException is thrown.

Example1:Use ArrayList addAll() to insert elements

import java.util.ArrayList;
class Main {
    public static void main(String[] args){
        
        //Create an arraylist
        ArrayList<Integer> primeNumbers = new ArrayList<>();
        //Add elements to the ArrayList
        primeNumbers.add(3);
        primeNumbers.add(5);
        System.out.println("Prime Numbers: " + primeNumbers);
        //Create another
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        // Add all elements from primeNumbers to numbers
        numbers.addAll(primeNumbers);
        System.out.println("Numbers: " + numbers);
    }
}

Output Result

Prime Numbers: [3, 5]
Numbers: [1, 2, 3, 5]

In the above example, we created two ArrayLists named primeNumbers and numbers. Note this line,

numbers.addAll(primeNumbers);

 Here, the addAll() method does not include the optional index parameter. Therefore, all elements of the ArrayList primeNumbers are added to the end of the ArrayList numbers.

NoteWe can use the add() method to add a single element to the ArrayList. For more information, please visitJava ArrayList add().

Example2:Insert elements into the specified position

import java.util.ArrayList;
class Main {
    public static void main(String[] args){
        ArrayList<String> languages1 = new ArrayList<>();
        languages1.add("Java");
        languages1.add("Python");
        System.out.println("ArrayList" 1: " + languages1);
        //Create another
        ArrayList<String> languages2 = new ArrayList<>();
        languages2.add("JavaScript");
        languages2.add("C");
        System.out.println("ArrayList" 2: " + languages2);
        // Add languages1to languages2elements are added to the index1at 
        languages2.addAll(1, languages1);
        System.out.println("Updated ArrayList" 2: " + languages2);
    }
}

Output Result

ArrayList 1: [Java, Python]
ArrayList 2: [JavaScript, C]
Updated ArrayList 2: [JavaScript, Java, Python, C]

In the above example, we have two named languages1and languages2of the ArrayList. Note this line,

languages2.addAll(1, languages1);

Here, addAll() includes an optional index parameter. Therefore, the arraylist languages1Add all elements in the languages index0th.

Example3:Insert elements from the collection into ArrayList

import java.util.ArrayList;
import java.util.HashSet;
class Main {
    public static void main(String[] args){
        //Create a string type hash set
        HashSet<String> set = new HashSet<>();
        //Add elements to the hashset
        set.add("Java");
        set.add("Python");
        set.add("JavaScript");
        System.out.println("HashSet: " + set);
        //Create an arraylist
        ArrayList<String> list = new ArrayList<>();
        //Add elements to the arraylist
        list.add("English");
        System.out.println("Initial ArrayList: " + list);
        //Add all elements from hashset to arraylist 
        list.addAll(set);
        System.out.println("Updated ArrayList: ", + list);
    }
}

Output Result

Set: [Java, JavaScript, Python]
Initial ArrayList: [English]
Updated ArrayList: [English, Java, JavaScript, Python]

In the above example, we created a hash set named hashset and an ArrayList named list. Note this line,

list.addAll(set);

Here, we use the addAll() method to add all elements from the hash set to the array list. There is no optional parameter index in the method. Therefore, all elements are added to the end of the array list.

Java ArrayList Methods