Collections static boolean addAll(Collection c,T... elements) method Example Program


Adds 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.
When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection:
Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
 

Program

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author : karthikeyan.T
 * @description : the following code shows how to use
 *              Collections.addAll(Collection <? super T> c, T... elements)
 *              method.
 * */
public class AddAll {
	public static void main(String[] args) {
		List<String> al = new ArrayList<String>();
		al.add("mohan");
		al.add("karthik");
		al.add("hari");
		al.add("kamal");
		al.add("anand");
		al.add("vinodh");
		System.out.println(al);
		Collections.addAll(al, "karthik", "mohan", "vinodh");
		System.out.println(al);
	}
}

Output

[mohan, karthik, hari, kamal, anand, vinodh]
[mohan, karthik, hari, kamal, anand, vinodh, karthik, mohan, vinodh]

Explanation

public static <T> boolean addAll(Collection<? super T> c,T... elements)
Adds 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.
When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection:
     Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
 
Parameters:
c - the collection into which elements are to be inserted
elements - the elements to insert into c
Returns:
true if the collection changed as a result of the call
Throws:
UnsupportedOperationException - if c does not support the add operation
NullPointerException - if elements contains one or more null values and c does not permit null elements, or if c or elements are null
IllegalArgumentException - if some property of a value in elements prevents it from being added to c
Since:
1.5
See Also:
Collection.addAll(Collection)


Related Post

Comments


©candidjava.com