Open In App
Related Articles

TreeMap ceilingEntry() and ceilingKey() methods in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

There are two variants of ceilingEntry() in Java.util.TreeMap, both are discussed in this article.

1. ceilingEntry(K Key) : It is used to return 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.

Syntax : 
public Map.Entry ceilingEntry(K key)
Parameters : 
key : The key to be matched.
Return Value : 
It returns the entry with the least key greater than or equal to key, and null if 
there is no such key.
Exception : 
ClassCastException : It throws the exception if the specified key cannot be compared
with the keys currently in the map.
NullPointerException : It throws the exception if the specified key is null. 




// Java code to demonstrate the working of 
// ceilingEntry()
  
import java.io.*;
import java.util.*;
  
public class ceilingEntry1 {
      
  public static void main(String[] args) {
   
  // Declaring the tree map of Integer and String
  TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
       
  // assigning the values in the tree map
  // using put()
  treemap.put(2, "two");
  treemap.put(7, "seven");
  treemap.put(3, "three");
     
  // Use of ceilingEntry()
  // returns 7=seven ( next greater key-value)
  System.out.println("The next greater key-value of 5 is : " + treemap.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 : " + treemap.ceilingEntry(8));
    
  }
}


Output:

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

2. ceilingKey(K key) : This has also the same work as that of the upper one but the only difference is that it does not contains the mapped-keys.It simply returns the least key greater or equal to the given key, else returns NULL.

Syntax : 
public K ceilingKey(K key)
Parameters : 
key : The key to be matched.
Return Value : 
It returns the entry with the least key greater than or equal to key, and null 
if there is no such key.
Exception:
ClassCastException : It throws the exception if the specified key cannot be compared
with the keys currently in the map.
NullPointerException : It throws the exception if the specified key is null. 




// Java code to demonstrate the working of 
// ceilingKey()
  
import java.io.*;
import java.util.*;
  
public class ceilingKey1 {
      
  public static void main(String[] args) {
   
  // Declaring the tree map of Integer and String
  TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
       
  // assigning the values in the tree map
  // using put()
  treemap.put(2, "two");
  treemap.put(7, "seven");
  treemap.put(3, "three");
     
  // Use of ceilingKey()
  // returns 7 ( next greater key)
  System.out.println("The next greater key of 5 is : " + treemap.ceilingKey(5));
    
  // returns "null" as no key present
  // greater than or equal to number
  System.out.println("The next greater key of 8 is : " + treemap.ceilingKey(8));
    
  }
}


Output:

The next greater key of 5 is : 7
The next greater key of 8 is : null

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 18 Sep, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials