Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList addAll() Method

Posted on July 22, 2025July 22, 2025 By Admin No Comments on Java ArrayList addAll() Method

The addAll() method is a powerful and convenient feature of Java ArrayList class that allows you to add all elements from a specified collection to an existing ArrayList. Whether you want to append multiple elements at the end or insert them at a specific position, addAll() simplifies these operations with just one method call.

What is the addAll() Method?

The addAll() method is used to add every element from another collection (such as another ArrayList, LinkedList, or any class that implements the Collection interface) into the current ArrayList.

It comes in two common variants:

  • addAll(Collection<? extends E> c)
  • addAll(int index, Collection<? extends E> c)

Where E is the type of elements stored in the list.

Syntax

boolean addAll(Collection<? extends E> c)
boolean addAll(int index, Collection<? extends E> c)

Parameters

ParameterDescription
Collection<? extends E> cThe collection whose elements are to be added.
int index (optional) The position at which to insert the first element from the specified collection.

How does addAll() Works?

  • When called without an index, addAll() appends all elements of the provided collection to the end of the list.
  • When called with an index, it inserts all elements starting at the given position, shifting existing elements to the right.
  • The method returns true if the ArrayList changed as a result of the call.

Important Notes

  • If the specified index is out of bounds (less than 0 or greater than list size), it throws an IndexOutOfBoundsException.
  • If the specified collection is null, a NullPointerException is thrown.
  • The order of elements in the appended collection is preserved.

Examples of the addAll() Method

Example 1: Append Elements at the End

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("A");
        list1.add("B");
        list1.add("C");

        ArrayList<String> list2 = new ArrayList<>();
        list2.add("D");
        list2.add("E");

        // Append all elements of list2 to list1
        list1.addAll(list2);

        System.out.println(list1);
    }
}

Output:

[A, B, C, D, E]

Example 2: Insert Elements at a Specific Position

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("Red");
        list1.add("Green");
        list1.add("Blue");

        ArrayList<String> newColors = new ArrayList<>();
        newColors.add("Yellow");
        newColors.add("Orange");

        // Insert newColors starting at index 1
        list1.addAll(1, newColors);

        System.out.println(list1);
    }
}

Output:

[Red, Yellow, Orange, Green, Blue]

When to Use addAll() Method?

  • Merging two lists efficiently without looping manually.
  • Adding multiple elements at once, improving performance and code readability.
  • Inserting entire collections at a specific point in a list for flexible data structures.

Summary

AspectDetails
PurposeTo add a collection of elements to an ArrayList
Overloaded MethodsaddAll(Collection c), addAll(int index, Collection c)
Returnstrue if the list was modified
ThrowsIndexOutOfBoundsException, NullPointerException
Use CaseCombine lists, batch insertions

Conclusion

The ArrayList‘s addAll() method is an essential tool for handling bulk additions to lists in Java. It saves developers the trouble of writing manual loops and simplifies code while maintaining performance and order. Whether appending at the end or inserting at a specific index, addAll() lets you manage collections efficiently and cleanly.

Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList add() Method
Next Post: Java ArrayList clear() Method

Leave a Reply Cancel reply

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

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme