TreeMap higherKey() method in Java with Examples - GeeksforGeeks

08 Jun, 2021
The higherKey() method of java.util.TreeMap class is used to return the least key strictly greater than the given key, or null if there is no such key.Syntax:  
public K higherKey(K key)
Parameters: This method takes the key k as a parameter.Return Value: This method returns 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 higherKey() methodExample 1:  
Java
// Java program to demonstrate// higherKey() 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");             // printing the TreeMap            System.out.println("TreeMap: " + treemap);             // getting higher key value for 3            // using higherKey() method            int value = treemap.higherKey(3);             // printing the value            System.out.println("The higherKey value "                               + " for 3: " + value);        }         catch (NullPointerException e) {            System.out.println("Exception thrown : " + e);        }    }}
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
The higherKey value  for 3: 4
 
Example 2: For NullPointerException  
Java
// Java program to demonstrate// higherKey() 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");             // printing the TreeMap            System.out.println("TreeMap: " + treemap);             // getting higher key value for null            // using higherKey() method            System.out.println("Trying to get higherKey"                               + " value for null");             int value = treemap.higherKey(null);             // printing the value            System.out.println("Value is: " + value);        }         catch (NullPointerException e) {            System.out.println("Exception thrown : " + e);        }    }}
TreeMap: {1=One, 2=Two, 3=Three, 4=Four, 5=Five}
Trying to get higherKey value for null
Exception thrown : java.lang.NullPointerException
 
anikakapoor
Java - util package
Java-Library
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
HashMap in Java with Examples
Stream In Java
Interfaces in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java
Multithreading in Java