Wednesday, October 8, 2025
HomeLanguagesJavaNavigableMap lowerKey() Method in Java

NavigableMap lowerKey() Method in Java

The lowerKey() method of NavigableMap Interface 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 NavigableMap.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 programs illustrate the use of lowerKey() method:

Example 1:




// Java program to demonstrate lowerKey() method
  
import java.util.*;
  
public class FloorKeyDemo {
    public static void main(String args[])
    {
  
        // create an empty TreeMap
        NavigableMap<Integer, String>
            navMap = new TreeMap<Integer, String>();
  
        // Insert the values
        navMap.put(6, "Six");
        navMap.put(1, "One");
        navMap.put(5, "Five");
        navMap.put(3, "Three");
        navMap.put(8, "Eight");
        navMap.put(10, "Ten");
  
        // Print the Values of TreeMap
        System.out.println("TreeMap: " + navMap.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(navMap.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(navMap.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 program to demonstrate lowerKey() method
  
import java.util.*;
  
public class FloorKeyDemo {
    public static void main(String args[])
    {
  
        // create an empty TreeMap
        NavigableMap<Integer, String>
            navMap = new TreeMap<Integer, String>();
  
        // Insert the values
        navMap.put(6, "Six");
        navMap.put(1, "One");
        navMap.put(5, "Five");
        navMap.put(3, "Three");
        navMap.put(8, "Eight");
        navMap.put(10, "Ten");
  
        // Print the Values of TreeMap
        System.out.println("TreeMap: " + navMap.toString());
  
        try {
            // Passing null as parameter to lowerKey()
            // This will throw exception
            System.out.println(navMap.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
RELATED ARTICLES

Most Popular

Dominic
32341 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6783 POSTS0 COMMENTS
Umr Jansen
6786 POSTS0 COMMENTS