Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the TreeSet.contains() 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.contains() method of the TreeSet class checks whether a particular element is present in the TreeSet object or not.

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

import java.util.TreeSet

Parameters

The TreeSet.contains() method takes the following parameters:

  • element: The element to be checked for in a TreeSet object. The data type of this parameter should be the same as the data type of the TreeSet instance.

Return value

If the element is present in the TreeSet object, the TreeSet.contains() method returns true; otherwise, it returns false.

Example

Consider the following TreeSet object:

[1,2,5,7,8,41, 2, 5, 7, 8, 4]

  • If you check for element = 2, the TreeSet.contains() method returns true because 22 is present in the TreeSet object.
  • If you check for element = 5, the TreeSet.contains() method returns true because 55 is present in the TreeSet object.
  • If you check for element = 9, the TreeSet.contains() method returns false because 99 is not present in the TreeSet object.
  • If you check for element = 23, the TreeSet.contains() method returns false because 2323 is not present in the TreeSet object.

Code

The code below shows how the TreeSet.contains() 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(3);
tree_set.add(0);
System.out.println("Does tree_set contains 8 : "+tree_set.contains(8));
System.out.println("Does tree_set contains 24 : "+tree_set.contains(24));
System.out.println("Does tree_set contains 3 : "+tree_set.contains(3));
System.out.println("Does tree_set contains 86 : "+tree_set.contains(86));
}
}
Using TreeSet.contains() 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.

  • Lines 9 to 13 add elements into the TreeSet through the TreeSet.add() method.

  • In lines 15 to 18, the TreeSet.contains() method checks for the presence of a specific element, and the return value is displayed accordingly.

Similarly, the TreeSet.contains() method can check for the presence of a particular element in the TreeSet object.

RELATED TAGS

java
communitycreator

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