NavigableSet addAll() method in Java

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



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

Note: The addAll() method of NavigableSet interface in Java is inherited from the Set interface.

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

Program 1 : Appending a tree NavigableSet.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

NavigableSet: [4, Geeks, To, TreeNavigableSet, Welcome]
NavigableSet: [4, Geeks, Hello, To, TreeNavigableSet, Welcome, World]

Program 2 : Appending an ArrayList.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.