Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is the TreeSet.add() function in Java?

Vinisha Maheshwari

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The TreeSet.add() method of the TreeSet class in Java adds a particular element into a TreeSet object. TreeSet.add() only adds that particular element if the TreeSet object does not already contain it.

To use the TreeSet.add() method, you need to import the java.util package, as shown below.

import java.util.TreeSet

Parameters

  • element: The element to be added to the TreeSet object. The data type of this parameter should be the same as the data type of the TreeSet instance.

Return value

If element is not present in the TreeSet object, the TreeSet.add() method adds it to the TreeSet and returns true. Otherwise, TreeSet.add() does not add the element and returns false.

Example

Consider the following list of elements to be added to a TreeSet object:

  • [1,8,5,8,31, 8, 5, 8, 3]

The TreeSet.add() method would result in the TreeSet below:

  • [1,8,5,31, 8, 5, 3]

    • The first element is 11, which was not already in the TreeSet, so 11 is added to the TreeSet.

    • Similarly, the second element is 88, which was not already in the TreeSet, so 88 is added to the TreeSet.

    • The third element is 55, which was not already in the TreeSet, so 55 is added to the TreeSet.

    • The fourth element is 88, which was already present in the TreeSet, so 88 is not added to the TreeSet.

    • The final element is 33, which was not already in the TreeSet, so 33 is added to the TreeSet.

The TreeSet stores [1,3,5,8] in the sorted order.

Code

The code below shows how the TreeSet.add() method works in Java.

import java.io.*;
import java.util.TreeSet;
class Main
{
public static void main(String args[])
{
TreeSet<Integer> tree_set = new TreeSet<Integer>();
tree_set.add(1);
tree_set.add(8);
tree_set.add(5);
tree_set.add(8);
tree_set.add(3);
tree_set.add(0);
System.out.println("TreeSet: " + tree_set);
}
}
Using TreeSet.add() function in Java

Explanation

In the code above:

  • Lines 1 and 2 import the required packages and classes.

  • In line 4, we initiate the Main class.

  • In line 8, we initialize a TreeSet object of type int.

  • In lines 9 to 14, we add elements to the TreeSet object through the TreeSet.add() method.

  • In line 15, we display the final TreeSet.

RELATED TAGS

communitycreator
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring