Write a Java program to get the flooring item of specified value from TreeSet collection


The floor() method in TreeSet returns the greatest element in this set less than or equal to the given element, or null if there is no such element. In this code, the num TreeSet contains the elements {10, 20, 25, 30, 35}. The call num.floor(38) searches for the greatest element in the set less than or equal to 38. Since there is no element less than 38, the floor() method returns the greatest element in the set, which is 35.

Source Code

import java.io.*;
import java.util.*;
public class GetFlooring
{
	public static void main(String args[])
	{
		TreeSet <Integer> num = new TreeSet <Integer>();
		num.add(10);
		num.add(35);
		num.add(20);
		num.add(30);
		num.add(25);
 
		int value = num.floor(38);
		System.out.println("Flooring value for 38 : " + value);
	}
}

Output

Flooring value for 38 : 35