NavigableSet add() method in Java

The add() method of NavigableSet in Java is used to add a specific element into a NavigableSet collection. The function adds the element only if the specified element is not already present in the NavigableSet else the function return False if the element is already present in the NavigableSet.

Syntax:

boolean add(E element)

Where, E is the type of element maintained
by this NavigableSet collection.

Parameters: The parameter element is of the type of element maintained by this NavigableSet and it refers to the element to be added to the NavigableSet.



Return Value: The function returns True if the element is not present in the NavigableSet and is new, else it returns False if the element is already present in the NavigableSet.

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

Below programs illustrate the use of Java.util.NavigableSet.add() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

NavigableSet: [4, Geeks, NavigableSet, To, Welcome]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate add() method
import java.io.*;
import java.util.*;
  
public class NavigableSetDemo {
    public static void main(String args[])
    {
        // Creating an empty NavigableSet
        NavigableSet<Integer> s = new TreeSet<Integer>();
  
        // Use add() method to add elements into the NavigableSet
        s.add(10);
        s.add(20);
        s.add(30);
        s.add(40);
        s.add(50);
        s.add(60);
  
        // Displaying the NavigableSet
        System.out.println("NavigableSet: " + s);
    }
}

chevron_right


Output:

NavigableSet: [10, 20, 30, 40, 50, 60]

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html#add(E)



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.