Collections addAll() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.