TreeMap floorKey() in Java with Examples
Pre-requisite: TreeMap in Java
The floorKey() method is used to return the greatest key less than or equal to given key from the parameter.
Syntax:
public K floorKey(K key)
Parameter: This method accepts a mandatory parameter key which is the key to be matched.
Return Value: The method call returns the greatest key less than or equal to key, or null if there is no such key.
Exception: This method throws following exceptions:
- ClassCastException: When the specified key cannot be compared with the key available in Map.
- NullPointerException: When the specified key in map is null and it uses natural
ordering which means, comparator does not permit null keys.
Below are examples to illustrate the use of floorKey() method:
Example 1:
// Java program to demonstrate floorKey() method import java.util.TreeMap; public class FloorKeyDemo { public static void main(String args[]) { // create an empty TreeMap TreeMap<Integer, String> numMap = new TreeMap<Integer, String>(); // Insert the values numMap.put(6, "Six"); numMap.put(1, "One"); numMap.put(5, "Five"); numMap.put(3, "Three"); numMap.put(8, "Eight"); numMap.put(10, "Ten"); // Print the Values of TreeMap System.out.println("TreeMap: " + numMap.toString()); // Get the greatest key mapping of the Map // As here 11 is not available it returns 10 // because ten is less than 11 System.out.print("Floor Entry of Element 11 is: "); System.out.println(numMap.floorKey(11)); // This will give null System.out.print("Floor Entry of Element 0 is: "); System.out.println(numMap.floorKey(0)); } } |
TreeMap: {1=One, 3=Three, 5=Five, 6=Six, 8=Eight, 10=Ten}
Floor Entry of Element 11 is: 10
Floor Entry of Element 0 is: null
Example 2: To demonstrate NullPointerException
// Java program to demonstrate floorKey() method import java.util.TreeMap; public class FloorKeyDemo { public static void main(String args[]) { // create an empty TreeMap TreeMap<Integer, String> numMap = new TreeMap<Integer, String>(); // Insert the values numMap.put(6, "Six"); numMap.put(1, "One"); numMap.put(5, "Five"); numMap.put(3, "Three"); numMap.put(8, "Eight"); numMap.put(10, "Ten"); // Print the Values of TreeMap System.out.println("TreeMap: " + numMap.toString()); try { // Passing null as parameter to floorKey() // This will throw exception System.out.println(numMap.floorKey(null)); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
TreeMap: {1=One, 3=Three, 5=Five, 6=Six, 8=Eight, 10=Ten}
Exception: java.lang.NullPointerException
Recommended Posts:
- Java.util.TreeMap.floorEntry() and floorKey() in Java
- TreeMap ceilingKey() in Java with Examples
- TreeMap lowerKey() in Java with Examples
- TreeMap higherEntry() method in Java with Examples
- TreeMap higherKey() method in Java with Examples
- TreeMap lowerEntry() method in Java with Examples
- TreeMap comparator() method in Java with Examples
- TreeMap navigableKeySet() method in Java with Examples
- NavigableMap floorKey() method in Java
- Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
- Java.util.TreeMap.descendingMap() and descendingKeyset() in Java
- Java.util.TreeMap.containskey() and containsValue() in Java
- Java.util.TreeMap.firstEntry() and firstKey() in Java
- TreeMap in Java
- HashMap and TreeMap in Java
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.



