Wednesday, July 3, 2024
HomeLanguagesJavaNavigableMap higherEntry() method in Java

NavigableMap higherEntry() method in Java

The higherEntry() method of NavigableMap interface in Java 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 existed.

Syntax:

Map.Entry< K, V > higherEntry(K key)

Where, K is the type of key maintained by this map and V is the type of values mapped to the keys.

Parameters: This function accepts a single parameter Key which refers to the type of key maintained by this map container.

Return Value: It returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key existed.

Below programs illustrate the higherEntry() method in Java:

Program 1: When the key is integer.




// Java code to demonstrate the working of
// higherEntry() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the NavigableMap of Integer and String
        NavigableMap<Integer, String> nmmp = new TreeMap<>();
  
        // assigning the values in the NavigableMap
        // using put()
        nmmp.put(2, "two");
        nmmp.put(7, "seven");
        nmmp.put(3, "three");
  
        System.out.println("The mapping with least key is : "
                           + nmmp.higherEntry(2));
    }
}


Output:

The mapping with least key is : 3=three

Program 2: When the key is string.




// Java code to demonstrate the working of
// higherEntry() method
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the NavigableMap of Integer and String
        NavigableMap<String, String> tmmp = new TreeMap<>();
  
        // assigning the values in the NavigableMap
        // using put()
        tmmp.put("one", "two");
        tmmp.put("six", "seven");
        tmmp.put("two", "three");
  
        System.out.println("The mapping associated with the least key is : "
                           + tmmp.higherEntry("one"));
    }
}


Output:

The mapping associated with the least key is : six=seven

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableMap.html#higherEntry(K)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments