AbstractCollection addAll() Method in Java with Examples
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 randomnly 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 code to illustrate addAll() // method of AbstractCollection import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String args[]) { // Creating an empty collection AbstractCollection<String> abs1 = new TreeSet<String>(); // Use add() method to add // elements into the Collection abs1.add("Welcome"); abs1.add("To"); abs1.add("Geeks"); abs1.add("4"); abs1.add("Geeks"); abs1.add("TreeSet"); // Displaying the Collection System.out.println("AbstractCollection 1: " + abs1); // Creating anothe Collection AbstractCollection<String> abs2 = new TreeSet<String>(); // Displaying the Collection System.out.println("AbstractCollection 2: " + abs2); // Using addAll() method to Append abs2.addAll(abs1); // Displaying the Collection System.out.println("AbstractCollection 2: " + abs2); } } |
AbstractCollection 1: [4, Geeks, To, TreeSet, Welcome] AbstractCollection 2: [] AbstractCollection 2: [4, Geeks, To, TreeSet, Welcome]
Program 2:
// Java code to illustrate addAll() // method of AbstractCollection import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String args[]) { // Creating an empty collection AbstractCollection<Integer> abs1 = new TreeSet<Integer>(); // Use add() method to add // elements into the Collection abs1.add(10); abs1.add(20); abs1.add(30); abs1.add(40); abs1.add(50); // Displaying the Collection System.out.println("AbstractCollection 1: " + abs1); // Creating anothe Collection AbstractCollection<Integer> abs2 = new TreeSet<Integer>(); // Displaying the Collection System.out.println("AbstractCollection 2: " + abs2); // Using addAll() method to Append abs2.addAll(abs1); // Displaying the Collection System.out.println("AbstractCollection 2: " + abs2); } } |
AbstractCollection 1: [10, 20, 30, 40, 50] AbstractCollection 2: [] AbstractCollection 2: [10, 20, 30, 40, 50]
Recommended Posts:
- AbstractCollection contains() Method in Java with Examples
- AbstractCollection add() Method in Java with Examples
- AbstractCollection clear() Method in Java with Examples
- AbstractCollection toArray() Method in Java with Examples
- AbstractCollection toString() method in Java with Examples
- AbstractCollection retainAll() method in Java with Examples
- AbstractCollection remove() Method in Java with Examples
- AbstractCollection containsAll() Method in Java with Examples
- AbstractCollection isEmpty() Method in Java with Examples
- AbstractCollection size() Method in Java with Examples
- Set addAll() method in Java with Examples
- AbstractSequentialList addAll() Method in Java with Examples
- CopyOnWriteArraySet addAll() method in Java with Examples
- List addAll() Method in Java with Examples
- SortedSet addAll() 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.



