java.util.Collections.addAll() Method


Advertisements


Description

The addAll(Collection<? super T>, T..) method is used to add all of the specified elements to the specified collection.

Declaration

Following is the declaration for java.util.Collections.addAll() method.

public static <T> boolean addAll(Collection<? super T> c, T.. a)

Parameters

  • c − This is the collection into which elements are to be inserted.

  • a − This is the elements to insert into c

Return Value

The method call returns 'true' if the collection changed as a result of the call

Exception

  • UnsupportedOperationException − This is thrown if c does not support the add method.

  • NullPointerException − This is thrown if elements contains one or more null values and c does not support null elements.

  • IllegalArgumentException − This is thrown if some aspect of a value in elements prevents it from being added to c.

Example

The following example shows the usage of java.util.Collections.addAll()

Live Demo
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {
   
      // create array list object       
      List arrlist = new ArrayList();

      // populate the list
      arrlist.add("A");
      arrlist.add("B");
      arrlist.add("C");  

      System.out.println("Initial collection value: "+arrlist);

      // add values to this collection
      boolean b = Collections.addAll(arrlist, "1","2","3");

      System.out.println("Final collection value: "+arrlist);
   }    
}

Let us compile and run the above program, this will produce the following result.

Initial collection value: [A, B, C]
Final collection value: [A, B, C, 1, 2, 3]

java_util_collections.htm

Advertisements