Open In App

SortedSet add() method in Java with Examples

Last Updated : 30 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

boolean add(E element)

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

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

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

Note: This method of SortedSet is inherited from the Set interface in Java.

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

Program 1:




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


Output:

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

Program 2:




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


Output:

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

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



Previous Article
Next Article

Similar Reads

SortedSet equals() method in Java with Examples
The equals() method of java.util.SortedSet class is used to verify the equality of an Object with a SortedSet and compare them. The method returns true if the size of both the SortedSets are equal and both contain the same elements. Syntax: public boolean equals(Object o) Parameters: This method takes the object o as a parameter to be compared for
2 min read
SortedSet addAll() method in Java with Examples
The 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 t
2 min read
SortedSet isEmpty() method in Java with Examples
The java.util.SortedSet.isEmpty() method is used to check if a SortedSet is empty or not. It returns True if the SortedSet is empty otherwise it returns False. Syntax: boolean isEmpty() Parameters: This method does not take any parameter. Return Value: The method returns True if the SortedSet is empty else returns False. Note: The isEmpty() method
1 min read
SortedSet clear() method in Java with Examples
The clear() method is used to remove all the elements from a SortedSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The method does not take any parameter Return Value: The method
1 min read
SortedSet removeAll() method in Java with Examples
The removeAll() method of SortedSet interface is used to remove from this SortedSet all of its elements that are contained in the specified collection.Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from this SortedSet.Returns Value: This method returns true
3 min read
SortedSet contains() method in Java with Examples
The contains() method is used to check whether a specific element is present in the SortedSet or not. So basically it is used to check if a SortedSet contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of SortedSet. This is the element that needs to be tested if it is present in
2 min read
SortedSet iterator() method in Java with Examples
The java.util.SortedSet.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = sortedSetInstance.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the elements of t
1 min read
SortedSet remove() method in Java with Examples
The remove(Object O) method of SortedSet interface is used to remove a particular element from this SortedSet. Syntax: boolean remove(Object O) Parameters: The parameter O is of the type of element maintained by this SortedSet and specifies the element to be removed from the Set. Return Value: This method returns True if the specified element is pr
1 min read
SortedSet toArray() method in Java with Examples
The toArray() method of Java SortedSet is used to form an array of the same elements as that of the SortedSet. Basically, it copies all the element from a SortedSet to a new array. Syntax: Object[] toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements similar to the SortedSe
2 min read
SortedSet size() method in Java with Examples
The size() method of SortedSet interface is used to get the size of the SortedSet or the number of elements present in the SortedSet. Syntax: int size() Parameters: This method does not takes any parameter. Return Value: The method returns the size or the number of elements present in the SortedSet. Note: The size() method in SortedSet is inherited
1 min read
Practice Tags :