TreeMap higherEntry() method in Java with Examples

The higherEntry() method of java.util.TreeMap class is used to return a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.

Syntax:

public Map.Entry higherEntry(K key)

Parameters: This method takes the key as a parameter.

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

Exception: This method throws the 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 higherEntry() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// higherEntry() method
// for <Integer, String> value
  
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 higher entry value fro 3
            Map.Entry<Integer, String>
                value
                = treemap.higherEntry(3);
  
            // printing the value
            System.out.println("The higherEntry value "
                               + " for 3: " + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
The higherEntry value  for 3: 4=Four

Example 2: For NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// higherEntry() 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 higher entry value fro null
            System.out.println("Trying to get "
                               + "the higher entry value"
                               + " for null");
  
            Map.Entry<Integer, String>
                value
                = treemap.higherEntry(null);
  
            // printing the value
            System.out.println("Value is: " + value);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Trying to get the higher entry 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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.