The addAll(Collection C) method of Java AbstractCollection is used to append all of the elements from a mentioned collection to this collection. The elements are added randomly without following any specific order.
Syntax:
boolean addAll(Collection C)
Parameters: The parameter C is a collection of any type that is to be added to the collection.
Return Value: The method returns true if it successfully appends the elements of the collection C to the existing collection otherwise it returns False.
Below programs illustrate the AbstractCollection.addAll() method:
Program 1:
Java
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<String>
abs1 = new TreeSet<String>();
abs1.add("Welcome");
abs1.add("To");
abs1.add("Geeks");
abs1.add(" 4 ");
abs1.add("Geeks");
abs1.add("TreeSet");
System.out.println("AbstractCollection 1 : "
+ abs1);
AbstractCollection<String>
abs2 = new TreeSet<String>();
System.out.println("AbstractCollection 2 : "
+ abs2);
abs2.addAll(abs1);
System.out.println("AbstractCollection 2 : "
+ abs2);
}
}
|
Output:AbstractCollection 1: [4, Geeks, To, TreeSet, Welcome]
AbstractCollection 2: []
AbstractCollection 2: [4, Geeks, To, TreeSet, Welcome]
Program 2:
Java
import java.util.*;
import java.util.AbstractCollection;
public class AbstractCollectionDemo {
public static void main(String args[])
{
AbstractCollection<Integer>
abs1 = new TreeSet<Integer>();
abs1.add( 10 );
abs1.add( 20 );
abs1.add( 30 );
abs1.add( 40 );
abs1.add( 50 );
System.out.println("AbstractCollection 1 : "
+ abs1);
AbstractCollection<Integer>
abs2 = new TreeSet<Integer>();
System.out.println("AbstractCollection 2 : "
+ abs2);
abs2.addAll(abs1);
System.out.println("AbstractCollection 2 : "
+ abs2);
}
}
|
Output:AbstractCollection 1: [10, 20, 30, 40, 50]
AbstractCollection 2: []
AbstractCollection 2: [10, 20, 30, 40, 50]