NavigableSet add() method in Java Last Updated : 30 Sep, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // 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); } } Output: NavigableSet: [4, Geeks, NavigableSet, To, Welcome] Program 2: Java // 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); } } Output: NavigableSet: [10, 20, 30, 40, 50, 60] Reference: https://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html#add(E) Comment More infoAdvertise with us Next Article NavigableSet addAll() method in Java G gopaldave Follow Improve Article Tags : Java Java-Functions Java-NavigableSet Practice Tags : Java Similar Reads 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 collect 2 min read TreeSet add() Method in Java The Java.util.TreeSet.add() method in Java TreeSet is used to add a specific element into a TreeSet. The function adds the element only if the specified element is not already present in the set else the function return False if the element is not present in the TreeSet. Syntax: Tree_Set.add(Object 1 min read NavigableMap put() Method in Java The put() method of NavigableMap Interface is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair 3 min read Vector add() Method in Java To add an element to a Vector in Java, you can use the add() method, which appends an element to the end of the vector.Implementation:Java// Java code to illustrate Adding // Element in Vector import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Vector 3 min read Set addAll() Method in Java In Java, the addAll() method of the Set class is used to add all the elements of a specified collection to the current collection. The elements are added randomly without following any specific order.Example 1: This example demonstrates how to merge two TreeSet using the addAll() method.Java// Java 2 min read 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 typ 2 min read Like