VOOZH about

URL: https://www.geeksforgeeks.org/java/abstractcollection-addall-method-in-java-with-examples/

⇱ AbstractCollection addAll() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

AbstractCollection addAll() Method in Java

Last Updated : 11 Jul, 2025

The addAll() method of Java AbstractCollection is used to append all elements from a given collection to the current collection. If the collection being appended is a TreeSet, the elements are stored in sorted order, as TreeSet maintains a natural ordering. It is important to note that AbstractCollection cannot be instantiated directly, so we use Collection as the type instead. 

Example 1: Adding Elements from One Collection to Another

In this example, we append all the elements from one collection to another using the addAll() method.


Output
Collection 1: [4, Geeks, To, TreeSet, Welcome]
Collection 2 (before addAll): []
Collection 2 (after addAll): [4, Geeks, To, TreeSet, Welcome]

Syntax of AbstractCollection addAll()

boolean addAll(Collection c);

  • Parameters: The parameter "c" is a collection of any type that is to be added to the collection. 
  • Return Value: The method returns "true" if it successfully appends the elements of collection c to the existing collection otherwise, it returns "false".
  • Exceptions:
    • NullPointerException: If the specified collection is null.
    • UnsupportedOperationException: If the collection does not support the add() operation. 

Example 2: Using addAll() method with integer value


Output
Collection 1: [10, 20, 30, 40, 50]
Collection 2 (before addAll): []
Collection 2 (after addAll): [10, 20, 30, 40, 50]
Comment