Skip to content
Related Articles

Related Articles

NavigableSet floor() method in Java
  • Last Updated : 29 Sep, 2018

The floor() method of NavigableSet interface in Java is used to return the greatest element in this set less than or equal to the given element, or null if there is no such element exists in the set.

Syntax:

E floor(E ele)

Where, E is the type of elements maintained by this Set container.

Parameters: This function accepts a parameter ele which refers to type of element maintained by this Set container.

Return Value: It returns the greatest element in this set less than or equal to the given element, or null if there is no such element.



Below programs illustrate the ceiling() method in Java:

Program 1: NavigableSet with integer elements.




// A Java program to demonstrate floor()
// method of NavigableSet
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<Integer> ns = new TreeSet<>();
        ns.add(0);
        ns.add(1);
        ns.add(2);
        ns.add(3);
        ns.add(4);
        ns.add(5);
        ns.add(6);
  
        System.out.println("Greatest element less than"
                   + " or equal to 4 is: " + ns.floor(4));
    }
}
Output:
Greatest element less than or equal to 4 is: 4

Program 2: NavigableSet with string elements.




// A Java program to floor()
// method of NavigableSet
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<String> ns = new TreeSet<>();
        ns.add("A");
        ns.add("B");
        ns.add("C");
        ns.add("D");
        ns.add("E");
        ns.add("F");
        ns.add("G");
  
        System.out.println("Greatest element less than"
                 + " or equal to D is: " + ns.floor("D"));
    }
}
Output:
Greatest element less than or equal to 4 is: D

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableSet.html#floor(E)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :