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:
// 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); } } } |
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); } } } |
arrlist before operation : [A, B, C, Tajmahal] Trying to add the null value with arrlist Exception thrown : java.lang.NullPointerException
Recommended Posts:
- Set addAll() method in Java with Examples
- SortedSet addAll() method in Java with Examples
- AbstractCollection addAll() Method in Java with Examples
- ConcurrentLinkedDeque addAll() method in Java with Examples
- CopyOnWriteArrayList addAll() method in Java with Examples
- AbstractSequentialList addAll() Method in Java with Examples
- AbstractQueue addAll() method in Java with examples
- CopyOnWriteArraySet addAll() method in Java with Examples
- List addAll() Method in Java with Examples
- AbstractList addAll() method in Java with Examples
- Collection addAll() method in Java with Examples
- LinkedBlockingDeque addAll() method in Java with Examples
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections copy() method in Java with Examples
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


