NavigableMap ceilingEntry() method in Java

The cielingEntry() method of NavigableMap interface in Java is used to returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key exists.

Syntax:

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

Parameters: It accepts a single parameter Key which is the key to be mapped.



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

Below programs illustrate the ceilingEntry() method in Java:

Program 1: When the key is integer.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working of
// ceilingEntry()  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> navmap = new TreeMap<>();
  
        // assigning the values in the NavigableMap
        // using put()
        navmap.put(2, "two");
        navmap.put(7, "seven");
        navmap.put(3, "three");
  
        // Use of ceilingEntry()
        // returns 7=seven ( next greater key-value)
        System.out.println("The next greater key-value of 5 is : "
                           + navmap.ceilingEntry(5));
  
        // returns "null" as no value present
        // greater than or equal to number
        System.out.println("The next greater key-value of 8 is : "
                                      + navmap.ceilingEntry(8));
    }
}

chevron_right


Output:

The next greater key-value of 5 is : 7=seven
The next greater key-value of 8 is : null

Program 2: When the key is string.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate the working of
// ceilingEntry()  method
  
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the NavigableMap of String and String
        NavigableMap<String, String> navmap = new TreeMap<String, String>();
  
        // assigning the values in the NavigableMap
        // using put()
        navmap.put("one", "Geeks");
        navmap.put("two", "for");
        navmap.put("three", "Geeks");
  
        // Use of ceilingEntry()
        // returns one = Geeks ( next greater key-value of "a")
        System.out.println("The next greater key-value of a is : "
                           + navmap.ceilingEntry("a"));
  
        // returns three = Geeks
        System.out.println("The next greater key-value of p is : "
                                       + navmap.ceilingEntry("p"));
    }
}

chevron_right


Output:

The next greater key-value of a is : one=Geeks
The next greater key-value of p is : three=Geeks

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



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.