Check if a particular value exists in Java TreeSet



TreeSet is a class in Java that implements the Set interface. It stores unique values in a sorted order. It is similar to HashSet, but the difference is that TreeSet stores elements in a sorted manner, while HashSet does not guarantee any order.

Our task is to check if a particular value exists in a TreeSet in Java. We can do this using the following methods:

Using contains() Method

The contains() method of the TreeSet class is used for checking if a particular value exists in the TreeSet. It returns true if the value is available in the TreeSet; otherwise, it returns false.

Syntax

Following is the syntax of the contains() method:

TreeSet<Type> set = new TreeSet<>();
boolean exists = set.contains(value);

In the above syntax, set is the object of the TreeSet class, value is the value we want to check, and exists is a boolean variable that will be true if the value exists in the TreeSet, otherwise false.

Example

In the below example, we will create a TreeSet of Integer type, and then we will check if the value = 5 exists in the TreeSet using the contains() method:

import java.util.TreeSet;
public class CheckIfValueExists {
   public static void main(String[] args){
      TreeSet<Integer> set = new TreeSet<>();
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);

      int valueToCheck = 5;

      if(set.contains(valueToCheck)) {
         System.out.println("Value exists in the TreeSet");
      } else {
         System.out.println("Value does not exist in the TreeSet");
      }
   }
}

When you run the above code, it will produce the following output:

Value exists in the TreeSet

Using Streams API

Streams API was introduced in Java 8, It provides methods that help us to process collections. We can use the anyMatch() method of the Stream interface to check if a particular value exists in the TreeSet.

Example

In the below example, we will create a TreeSet, next we will convert the TreeSet to a Stream using the stream() method, and then we will use the anyMatch() method to check if a value exists or not.

import java.util.TreeSet;
import java.util.stream.Stream;
public class CheckIfValueExists {
   public static void main(String[] args){
      TreeSet<Integer> set = new TreeSet<>();
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);
      int valueToCheck = 6;
      boolean exists = set.stream().anyMatch(value -> value == valueToCheck);
      if(exists) {
         System.out.println("Value exists in the TreeSet");
      } else {
         System.out.println("Value does not exist in the TreeSet");
      }
   }
}

When you run the above code, it will produce the following output:

Value does not exist in the TreeSet

Conclusion

In this article, we learned what a TreeSet is in Java. Then, we checked if a particular value exists in a TreeSet using the contains() method and the Streams API.

Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-07-24T18:33:24+05:30

717 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements