Skip to content
Related Articles

Related Articles

TreeMap lowerEntry() method in Java with Examples
  • Last Updated : 11 Oct, 2018
GeeksforGeeks - Summer Carnival Banner

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 key

Syntax:

public Map.Entry lowerEntry(K key)

Parameters: This method takes the key as a parameter for which the lower entry is to be found.

Return Value: This method returns an entry with the greatest key less than key, or null if there is no such key.

Exception: This method throws NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys.



Below are the examples to illustrate the lowerEntry() method

Example 1:




// Java program to demonstrate
// lowerEntry() method
// for <Integer, String>
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of TreeMap<Integer, String>
            TreeMap<Integer, String>
                treemap = new TreeMap<Integer, String>();
  
            // populating tree map
            treemap.put(1, "One");
            treemap.put(2, "Two");
            treemap.put(3, "Three");
            treemap.put(4, "Four");
            treemap.put(5, "Five");
  
            // pritnig the TreeMap
            System.out.println("TreeMap: " + treemap);
  
            // getting lowerEntry value for 3
            // using lowerEntry() method
            Map.Entry<Integer, String>
                value
                = treemap.lowerEntry(3);
  
            // printing the value
            System.out.println("The lowerEntry value "
                               + " for 3: " + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
The lowerEntry value  for 3: 2=Two

Example 2: For NullPointerException




// Java program to demonstrate
// lowerEntry() method
// for NullPointerException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of TreeMap<Integer, String>
            TreeMap<Integer, String>
                treemap = new TreeMap<Integer, String>();
  
            // populating tree map
            treemap.put(1, "One");
            treemap.put(2, "Two");
            treemap.put(3, "Three");
            treemap.put(4, "Four");
            treemap.put(5, "Five");
  
            // pritnig the TreeMap
            System.out.println("TreeMap: " + treemap);
  
            // getting lowerEntry value for null
            // using lowerEntry() method
            System.out.println("Trying to get"
                               + " the lowerEntry value"
                               + " for value NULL");
  
            Map.Entry<Integer, String>
                value
                = treemap.lowerEntry(null);
  
            // printing the value
            System.out.println("Value is: " + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output:
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Trying to get the lowerEntry value for value NULL
Exception thrown : java.lang.NullPointerException

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 :