Java Program to Check Whether an Item Exists in a TreeSet Collection or Not

Java program to check whether an item exists in a TreeSet collection or not

// Java program to check an item exist in a 
// TreeSet collection

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) {
    TreeSet < Integer > tree = new TreeSet < Integer > ();

    tree.add(10);
    tree.add(20);
    tree.add(25);
    tree.add(30);

    if (tree.contains(35))
      System.out.println("Item exists.");
    else
      System.out.println("Item does not exist.");

    if (tree.contains(25))
      System.out.println("Item exists.");
    else
      System.out.println("Item does not exist.");
  }
}

Output

Item does not exist.
Item exists.
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment