A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/collections-addall-method-in-java-with-examples/ below:

Collections addAll() method in Java with Examples

Collections addAll() method in Java with Examples

Last Updated : 20 May, 2019

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

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
// 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
// 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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4