Java Program to Get the Elements From the TreeSet Collection That Are Less Than the Specified Item

You are currently viewing Java Program to Get the Elements From the TreeSet Collection That Are Less Than the Specified Item

Java program to get the elements from the TreeSet collection that are less than the specified item

// Java program to get the elements from the TreeSet collection 
// that are less than the specified item

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

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

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

    TreeSet < Integer > headset = (TreeSet < Integer > ) tree.headSet(27);

    System.out.println("Headset is: " + headset);
  }
}

Output

Headset is: [10, 20, 25]

Ranjith

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

Leave a Reply