Monday, September 23, 2024
Google search engine
HomeLanguagesJavaNavigableMap higherKey() Method in Java

NavigableMap higherKey() Method in Java

The higherKey() method of NavigableMap interface 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() method:

Example 1:  

Java




// Java program to demonstrate
// higherKey() method
// for <Integer, String>
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] args) throws Exception
    {
        try {
 
            // Creating object of NavigableMap
            NavigableMap<Integer, String>
                navmap = new TreeMap<Integer, String>();
 
            // Populating the map
            navmap.put(1, "One");
            navmap.put(2, "Two");
            navmap.put(3, "Three");
            navmap.put(4, "Four");
            navmap.put(5, "Five");
 
            // Printing the TreeMap
            System.out.println("NavigableMap: " + navmap);
 
            // Getting higher key value for 3
            // using higherKey() method
            int value = navmap.higherKey(3);
 
            // Printing the value
            System.out.println("The higherKey value "
                               + " for 3: " + value);
        }
 
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

NavigableMap: {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[] args) throws Exception
    {
        try {
 
            // Creating object of TreeMap<Integer, String>
            NavigableMap<Integer, String>
                navmap = new TreeMap<Integer, String>();
 
            // Populating tree map
            navmap.put(1, "One");
            navmap.put(2, "Two");
            navmap.put(3, "Three");
            navmap.put(4, "Four");
            navmap.put(5, "Five");
 
            // Printing the NavigableMap
            System.out.println("TreeMap: " + navmap);
 
            // Getting higher key value for null
            // Using higherKey() method
            System.out.println("Trying to get higherKey"
                               + " value for null");
 
            int value = navmap.higherKey(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 higherKey value for null
Exception thrown : java.lang.NullPointerException

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments