Saturday, September 6, 2025
HomeLanguagesJavaTreeSet addAll() Method in Java

TreeSet addAll() Method in Java

The java.util.TreeSet.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 tree set. 

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

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

Program 1 : Appending a tree set. 

Java




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


Output:

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

Program 2 : Appending an ArrayList. 

Java




// Java code to illustrate addAll()
import java.io.*;
import java.util.TreeSet;
import java.util.ArrayList;
 
public class TreeSetDemo {
    public static void main(String args[])
    {
        // Creating an empty TreeSet
        TreeSet<String> tree = new TreeSet<String>();
 
        // Use add() method to add elements into the Set
        tree.add("Welcome");
        tree.add("To");
        tree.add("Geeks");
        tree.add("4");
        tree.add("Geeks");
        tree.add("TreeSet");
 
        // Displaying the TreeSet
        System.out.println("TreeSet: " + tree);
 
        // 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
        tree.addAll(collect);
 
        // Displaying the final tree
        System.out.println("Final TreeSet: " + tree);
    }
}


Output:

TreeSet: [4, Geeks, To, TreeSet, Welcome]
Final TreeSet: [4, A, Computer, Geeks, Portal, To, TreeSet, Welcome]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS