Thursday, September 4, 2025
HomeLanguagesJavaSet addAll() method in Java with Examples

Set addAll() method in Java with Examples

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)

RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS