TreeMap lowerKey() in Java with Examples Last Updated : 17 Sep, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report Pre-requisite: TreeMap in Java The lowerKey() method is used to return the greatest key strictly less than to given key, passed as the parameter. In simpler words, this method is used to find the next greatest element after the element passed as the parameter. Syntax: public K TreeMap.lowerKey(K key) Parameters: This method takes a mandatory parameter key which is this is the key to be matched. Return Value: This method returns the greatest key strictly less than 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 lowerKey() method: Example 1: Java // Java program to demonstrate lowerKey() 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 9 is not available it returns 8 // because 9 is strictly less than 11, present System.out.print("Lower Key Entry of Element 9 is: "); System.out.println(numMap.lowerKey(9)); // Though, 3 is available in the Map // it returns 1 because this method returns // strictly less than key according to the specified key System.out.print("Lower Key Entry of Element 3 is: "); System.out.println(numMap.lowerKey(3)); } } Output: TreeMap: {1=One, 3=Three, 5=Five, 6=Six, 8=Eight, 10=Ten} Lower Key Entry of Element 9 is: 8 Lower Key Entry of Element 3 is: 1 Example 2: To demonstrate NullPointerException Java // Java program to demonstrate lowerKey() 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 lowerKey() // This will throw exception System.out.println(numMap.lowerKey(null)); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: TreeMap: {1=One, 3=Three, 5=Five, 6=Six, 8=Eight, 10=Ten} Exception: java.lang.NullPointerException Comment More infoAdvertise with us Next Article TreeMap lowerEntry() method in Java with Examples B bilal-hungund Follow Improve Article Tags : Java Java - util package Java-Functions java-TreeMap Practice Tags : Java Similar Reads TreeMap lowerEntry() method in Java with Examples The lowerEntry() method of java.util.TreeMap class is used to return a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such keySyntax: public Map.Entry lowerEntry(K key) Parameters: This method takes the key as a parameter for which the low 2 min read TreeMap keySet() Method in Java with Examples In Java, keySet() method of TreeMap class is present inside java.util package in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in ascending order. Since the set 3 min read TreeMap size() Method in Java with Examples size() method of TreeMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. --> java.util package --> TreeMap class --> size() Method Syntax: Tree_Map.size() Return Value: The size of the map which also means the number of key-v 2 min read TreeMap lastEntry() Method in Java with Examples The java.util.TreeMap.lastEntry() method is used to returns the key-value mapping associated with the greatest key in this map, or null if the map is empty. Syntax: tree_map.lastEntry() Parameters: The method does not take any parameters. Return Value: The method returns an entry with the greatest k 2 min read TreeMap navigableKeySet() method in Java with Examples The navigableKeySet() method of java.util.TreeMap class is used to return a NavigableSet view of the keys contained in this map.The set's iterator returns the keys in ascending order. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified 2 min read TreeMap clone() Method in Java with Examples In Java, clone() method of the TreeMap class is used to return a shallow copy of the mentioned treemap. It just creates a copy of the map. --> java.util Package --> TreeMap Class --> clone() Method Syntax: Tree_Map.clone() Parameters: The method does not take any parameters. Return Type: A 2 min read Like