Write a Java program to get the strictly greater element from TreeSet collection based on a given item


This Java program defines a class named Strictly_Greater which contains a main method that creates a TreeSet of integers and adds some elements to it. The program then calls the higher method on the TreeSet with an argument of 55, which returns the smallest element in the set that is strictly greater than 55. This value is then printed to the console using the println method.

In this particular case, the TreeSet contains the elements {20, 30, 50, 60, 70}. The smallest element in the set that is strictly greater than 55 is 60, so the program outputs:

Source Code

import java.io.*;
import java.util.*;
public class Strictly_Greater
{
	public static void main(String args[])
	{
		TreeSet <Integer> num = new TreeSet <Integer>();
		num.add(70);
		num.add(30);
		num.add(50);
		num.add(20);
		num.add(60);
 
		int higher = num.higher(55);
		System.out.println("Strictly Higher Element : " + higher);
	}
}

Output

Strictly Higher Element : 60