The Wayback Machine - https://web.archive.org/web/20240926141513/https://www.geeksforgeeks.org/collections-addall-method-in-java-with-examples/
Open In App

Collections addAll() method in Java with Examples

Last Updated : 20 May, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The addAll() method of java.util.Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

Syntax:

public static  boolean 
    addAll(Collection c, T... elements)

Parameters: This method takes the following argument as a parameter

  • c- the collection into which elements are to be inserted
  • elements- the elements to insert into c

Return Value: This method returns true if the collection changed as a result of the call.

Exception: This method throws NullPointerException if elements contains one or more null values and c does not permit null elements, or if c or elements are null

Below are the examples to illustrate the addAll() method

Example 1:




// Java program to demonstrate
// addAll() method
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of List<String>
            List<String> arrlist = new ArrayList<String>();
  
            // Adding element to arrlist
            arrlist.add("A");
            arrlist.add("B");
            arrlist.add("C");
            arrlist.add("Tajmahal");
  
            // printing the arrlist before operation
            System.out.println("arrlist before operation : " + arrlist);
  
            // add the specified element to specified Collections
            // using addAll() method
            boolean b = Collections.addAll(arrlist, "1", "2", "3");
  
            // printing the arrlist after operation
            System.out.println("result : " + b);
  
            // printing the arrlist after operation
            System.out.println("arrlist after operation : " + arrlist);
        }
  
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

arrlist before operation : [A, B, C, Tajmahal]
result : true
arrlist after operation : [A, B, C, Tajmahal, 1, 2, 3]

Output:

arrlist before operation : [A, B, C, Tajmahal]
result : true
arrlist after operation : [A, B, C, Tajmahal, 1, 2, 3]

Example 2: For NullPointerException




// Java program to demonstrate
// addAll() method
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating object of List<String>
            List<String> arrlist = new ArrayList<String>();
  
            // Adding element to arrlist
            arrlist.add("A");
            arrlist.add("B");
            arrlist.add("C");
            arrlist.add("Tajmahal");
  
            // printing the arrlist before operation
            System.out.println("arrlist before operation : " + arrlist);
  
            // add the specified element to specified Collections
            // using addAll() method
            System.out.println("\nTrying to add the null value with arrlist");
            boolean b = Collections.addAll(null, arrlist);
  
            // printing the arrlist after operation
            System.out.println("result : " + b);
  
            // printing the arrlist after operation
            System.out.println("arrlist after operation : " + arrlist);
        }
  
        catch (NullPointerException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

arrlist before operation : [A, B, C, Tajmahal]

Trying to add the null value with arrlist
Exception thrown : java.lang.NullPointerException


Similar Reads

AbstractList addAll() method in Java with Examples
The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that th
4 min read
AbstractQueue addAll() method in Java with examples
The addAll(E e) method of AbstractQueue adds all of the elements in the specified collection to this queue. Syntax: public boolean addAll(Collection c) Parameters: This method accepts a mandatory parameter collection containing elements to be added to this queue Returns: The method returns true if this queue changed as a result of the call Exceptio
3 min read
AbstractCollection addAll() Method in Java with Examples
The addAll(Collection C) method of Java AbstractCollection is used to append all of the elements from a mentioned collection to this collection. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the collection. Ret
2 min read
AbstractSequentialList addAll() Method in Java with Examples
The addAll(int index, Collection C) method of AbstractSequentialList is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a abstract sequential list. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in the above sy
2 min read
Collection addAll() method in Java with Examples
The addAll(Collection collection) of java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false. Syntax: Collection.addAll(Collection&lt;E&gt; collection)
3 min read
ConcurrentLinkedDeque addAll() method in Java with Examples
The addAll(Collection col) of ConcurrentLinkedDeque which takes col as a parameter, where col is a Collection of elements (List, ArrayList, LinkedList etc). This entire Collection gets appended or added to the end of the Dequeue. This method just like add() method returns true if the Collection gets appended to the Deque. Syntax: public boolean add
3 min read
Set addAll() method in Java with Examples
The java.util.Set.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the set. Return Value: The me
2 min read
List addAll() Method in Java with Examples
This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. Syntax: boolean addAll(Collection c) Parameters: This function has a single parameter, i.e, Collection c, whose elements are to be appended to the list. Returns: It returns true if
2 min read
CopyOnWriteArraySet addAll() method in Java with Examples
The addAll() method of CopyonWriteArraySet method adds all the element of the specified collection to this CopyOnWriteArraySet which are not present in it. This methods results the union of the two collections. Syntax: public boolean addAll(Collection&lt;E&gt; c) Parameters: This method accepts a parameter c which is the collection containing eleme
2 min read
CopyOnWriteArrayList addAll() method in Java with Examples
boolean addAll(Collection c) This method is used to append all the elements identified by the collection c at the end of the list, in the same order returned by the collection's iterator. Parameters: This method accepts a mandatory parameter c which is the collection containing the elements, which are to be appended at the end of the list. Return V
4 min read
LinkedBlockingDeque addAll() method in Java with Examples
The addAll() method of LinkedBlockingDeque appends all of the elements of the specified collection to the end of this deque.Syntax: public void addAll(Collection&lt;E&gt; c) Parameters: This method accepts a mandatory parameter c which is the collection to be inserted in the end of the LinkedBlockingDeque.Return Value: This method does not returns
2 min read
SortedSet addAll() method in Java with Examples
The addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the set. Return Value: The method returns t
2 min read
CompositeName addAll() method in Java with Examples
The addAll() method of a javax.naming.CompositeName class is used to add all the component of a different composite name object to this CompositeName object.There are two different addAll() methods. 1. addAll(int, Name): This method is used to add all the component of a different composite name object which is passed as a parameter at a specified p
3 min read
CompoundName addAll() method in Java with Examples
The addAll() method of a javax.naming.CompoundName class is used to add all the components of a different compound name object to this CompoundName object. There are two different addAll() methods. 1. addAll(int, Name): This method is used to add All the components of a different Compound name object which is passed as a parameter at a specified po
3 min read
Java.util.ArrayList.addall() method in Java
Below are the addAll() methods of ArrayList in Java: boolean addAll(Collection c) : This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behavior of this operation is undefined if the specified collection is modified while the ope
4 min read
Difference between Traditional Collections and Concurrent Collections in java
We all know about Traditional Collections ( i.e. List, Set, Queue and its implemented Classes) and Concurrent Collection (i.e. ConcurrentMap interface, ConcurrentHashMap class, CopyOnWriteArrayList class etc). In these two Collections, there are few differences like: Most of the Classes which are present in Traditional Collections (i.e ArrayList, L
3 min read
LinkedList addAll() Method in Java
java.util.LinkedList.addAll(Collection C): This method is used to append all of the elements from the collection passed as parameter to this function to the end of a list keeping in mind the order of return by the collections iterator. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of ArrayList. It is the collectio
3 min read
TreeSet addAll() Method in Java
The java.util.TreeSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the tree set. Return Valu
2 min read
Vector addAll() Method in Java
java.util.Vector.addAll(Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection
2 min read
ArrayDeque addAll() method in Java
The addAll() method of ArrayDeque is used to insert all the elements of the collection passed as parameter at the end of this ArrayDeque. For adding elements of a collection to ArrayDeque we have to iterate through the collection and add each element in ArrayDeque by using addAll(E e) method. This method works same as we discussed here but with les
4 min read
ConcurrentLinkedQueue addAll() method in Java
The addAll() method of ConcurrentLinkedQueue is used to inserts all the elements of the Collection, passed as parameter to this method, at the end of a ConcurrentLinkedQueue. The insertion of element is in same order as returned by the collections iterator. Syntax: public boolean addAll(Collection&lt;? extends E&gt; c) Parameter: This method takes
4 min read
Stack addAll(int, Collection) method in Java with Example
The addAll(int, Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a Stack. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in the above syntax and are described below. index: T
2 min read
Stack addAll(Collection) method in Java with Example
The addAll(Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function to the end of a Stack keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection of Arr
2 min read
NavigableSet addAll() method in Java
The java.util.NavigableSet.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing NavigableSet. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the Navigab
2 min read
Java.util.Collections.rotate() Method in Java with Examples
java.util.Collections.rotate() method is present in java.util.Collections class. It is used to rotate the elements present in the specified list of Collection by a given distance. Syntax: public static void rotate(List&lt; type &gt; list, int distance) Parameters : list - the list to be rotated. distance - the distance to rotate the list. type - Ty
2 min read
Java.util.Collections.disjoint() Method in java with Examples
java.util.Collections.disjoint() method is present in java.util.Collections class. It is used to check whether two specified collections are disjoint or not. More formally, two collections are disjoint if they have no elements in common. Syntax: public static boolean disjoint(Collection&lt;?&gt; c1, Collection&lt;?&gt; c2) Parameters : c1 - a colle
3 min read
Collections checkedMap() method in Java with Examples
The checkedMap() method of Collections class been present inside java.util package is used to return a dynamically typesafe view of the specified map. The returned map will be serializable if the specified map is serializable. Since null is considered to be a value of any reference type, the returned map permits the insertion of null keys or values
3 min read
Collections singletonMap() method in Java with Examples
The singletonMap() method of java.util.Collections class is used to return an immutable map, mapping only the specified key to the specified value. The returned map is serializable. Syntax: public static Map singletonMap(K key, V value) Parameters: This method takes the following parameters as a argument: key - the sole key to be stored in the retu
2 min read
Collections min() method in Java with Examples
min(Collection&lt;? extends T&gt; coll) The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (tha
4 min read
Collections max() method in Java with Examples
max(Collection&lt;? extends T&gt; coll) The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (tha
5 min read
Article Tags :
Practice Tags :