Write a Java program to get the strictly lower element from TreeSet collection


This Java program defines a class named Strictly_Lower which contains a main method that creates a TreeSet of integers and adds some elements to it. The program then calls the lower method on the TreeSet with an argument of 55, which returns the largest element in the set that is strictly less 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 largest element in the set that is strictly less than 55 is 50, so the program outputs:

Source Code

import java.io.*;
import java.util.*;
public class Strictly_Lower
{
	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 lower = num.lower(55);
		System.out.println("Strictly Lower Element : " + lower);
	}
}

Output

Strictly Lower Element : 50