The Wayback Machine - https://web.archive.org/web/20240618020152/https://www.geeksforgeeks.org/set-addall-method-in-java-with-examples/
Open In App

Set addAll() method in Java with Examples

Last Updated : 31 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.Set.addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. 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 set.

Return Value: The method returns true if it successfully appends the elements of the collection C to this Set otherwise it returns False.

Below programs illustrate the Java.util.Set.addAll() method:

Program 1 : Appending a tree set.




// Java code to illustrate addAll()
import java.io.*;
import java.util.*;
  
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty Set
        Set<String> st1 = new TreeSet<String>();
  
        // Use add() method to add elements into the Set
        st1.add("Welcome");
        st1.add("To");
        st1.add("Geeks");
        st1.add("4");
        st1.add("Geeks");
        st1.add("TreeSet");
  
        // Displaying the Set
        System.out.println("Set: " + st1);
  
        // Creating another Set
        Set<String> st2 = new TreeSet<String>();
  
        // Use add() method to add elements into the Set
        st2.add("Hello");
        st2.add("World");
  
        // Using addAll() method to Append
        st1.addAll(st2);
  
        // Displaying the final Set
        System.out.println("Set: " + st1);
    }
}


Output:

Set: [4, Geeks, To, TreeSet, Welcome]
Set: [4, Geeks, Hello, To, TreeSet, Welcome, World]

Program 2 : Appending an ArrayList.




// Java code to illustrate addAll()
import java.io.*;
import java.util.*;
  
public class SetDemo {
    public static void main(String args[])
    {
        // Creating an empty Set
        Set<String> st1 = new TreeSet<String>();
  
        // Use add() method to add elements into the Set
        st1.add("Welcome");
        st1.add("To");
        st1.add("Geeks");
        st1.add("4");
        st1.add("Geeks");
        st1.add("Set");
  
        // Displaying the Set
        System.out.println("Initial Set: " + st1);
  
        // An array collection is created
        ArrayList<String> collect = new ArrayList<String>();
        collect.add("A");
        collect.add("Computer");
        collect.add("Portal");
  
        // Using addAll() method to Append
        st1.addAll(collect);
  
        // Displaying the final Set
        System.out.println("Final Set: " + st1);
    }
}


Output:

Initial Set: [4, Geeks, Set, To, Welcome]
Final Set: [4, A, Computer, Geeks, Portal, Set, To, Welcome]

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#addAll(java.util.Collection)



Previous Article
Next Article

Similar Reads

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
3 min read
AbstractList addAll() method in Java with Examples
The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that th
4 min read
AbstractQueue addAll() method in Java with examples
The addAll(E e) method of AbstractQueue adds all of the elements in the specified collection to this queue. Syntax: public boolean addAll(Collection c) Parameters: This method accepts a mandatory parameter collection containing elements to be added to this queue Returns: The method returns true if this queue changed as a result of the call Exceptio
3 min read
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 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. Ret
2 min read
AbstractSequentialList addAll() Method in Java with Examples
The addAll(int index, Collection C) method of AbstractSequentialList is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a abstract sequential list. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in the above sy
2 min read
Collection addAll() method in Java with Examples
The addAll(Collection collection) of java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false. Syntax: Collection.addAll(Collection&lt;E&gt; collection)
3 min read
ConcurrentLinkedDeque addAll() method in Java with Examples
The addAll(Collection col) of ConcurrentLinkedDeque which takes col as a parameter, where col is a Collection of elements (List, ArrayList, LinkedList etc). This entire Collection gets appended or added to the end of the Dequeue. This method just like add() method returns true if the Collection gets appended to the Deque. Syntax: public boolean add
3 min read
List addAll() Method in Java with Examples
This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. Syntax: boolean addAll(Collection c) Parameters: This function has a single parameter, i.e, Collection c, whose elements are to be appended to the list. Returns: It returns true if
2 min read
CopyOnWriteArraySet addAll() method in Java with Examples
The addAll() method of CopyonWriteArraySet method adds all the element of the specified collection to this CopyOnWriteArraySet which are not present in it. This methods results the union of the two collections. Syntax: public boolean addAll(Collection&lt;E&gt; c) Parameters: This method accepts a parameter c which is the collection containing eleme
2 min read
CopyOnWriteArrayList addAll() method in Java with Examples
boolean addAll(Collection c) This method is used to append all the elements identified by the collection c at the end of the list, in the same order returned by the collection's iterator. Parameters: This method accepts a mandatory parameter c which is the collection containing the elements, which are to be appended at the end of the list. Return V
4 min read