TreeSet floor() method in Java with Examples

The floor() method of java.util.TreeSet<E> class 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.

Syntax:

public E floor(E e)

Parameters: This method takes the value e as a parameter which is to be matched.

Return Value: This method returns the greatest element less than or equal to e, or null if there is no such element

Exception: This method throws the NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

Below are the examples to illustrate the floor() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// floor() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // create tree set object
            TreeSet<Integer> treeadd = new TreeSet<Integer>();
  
            // populate the TreeSet using add() method
            treeadd.add(10);
            treeadd.add(20);
            treeadd.add(30);
            treeadd.add(40);
  
            // Print the TreeSet
            System.out.println("TreeSet: " + treeadd);
  
            // getting the floor value for 25
            // using floor() method
            int value = treeadd.floor(25);
  
            // printing the floor value
            System.out.println("Floor value for 25: "
                               + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

TreeSet: [10, 20, 30, 40]
Floor value for 25: 20

Example 2: for NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// floor() method
// for NullPointerException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
  
            // create tree set object
            TreeSet<Integer> treeadd = new TreeSet<Integer>();
  
            // populate the TreeSet using add() method
            treeadd.add(10);
            treeadd.add(20);
            treeadd.add(30);
            treeadd.add(40);
  
            // Print the TreeSet
            System.out.println("TreeSet: " + treeadd);
  
            // getting the floor value for null
            // using floor() method
            System.out.println("Trying to get"
                               + " the floor value"
                               + " for null");
  
            int value = treeadd.floor(null);
  
            // printing the floor value
            System.out.println("Floor value for 25: "
                               + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

TreeSet: [10, 20, 30, 40]
Trying to get the floor value for null
Exception thrown : java.lang.NullPointerException


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.