TreeMap ceilingKey() in Java with Examples
Last Updated :
18 Sep, 2018
The ceilingKey() function of TreeMap Class returns the least key greater than or equal to the given key or null if the such a key is absent.
Syntax:
public K ceilingKey(K key)
Parameters: This method accepts a mandatory parameter key which is the key to be searched for.
Return Value: This method returns the least key which is greater than or equal to the given key value.
If such a key is absent, null is returned.
Exceptions: This method throws following exceptions:
- ClassCastException – Thrown if the specified key can’t be compared with the given key values.
- NullPointerException – Thrown if the given key is null and the map uses natural ordering or the comparator does not permit null values.
Below are the examples to illustrate ceilingKey() method:
Program 1: To demonstrate use of ceilingKey() method for a TreeMap with comparator
import java.util.*;
public class Main {
public static void main(String[] args)
{
NavigableMap<Integer, String>
treemap = new TreeMap<Integer,
String>((a, b)
-> ((a > b)
? 1
: ((a == b)
? 0
: - 1 )));
treemap.put( 1 , " A " );
treemap.put( 2 , " B " );
treemap.put( 3 , " C " );
treemap.put( 4 , " D " );
treemap.put( 6 , " E " );
try {
System.out.println( "Ceiling key entry for 5: "
+ treemap.ceilingKey( 5 ));
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Ceiling key entry for 5: 6
Program 2: To demonstrate use of ceilingKey() method for a TreeMap without any comparator
import java.util.*;
public class Main {
public static void main(String[] args)
{
NavigableMap<Integer, String>
treemap = new TreeMap<Integer, String>();
treemap.put( 1 , " A " );
treemap.put( 2 , " B " );
treemap.put( 3 , " C " );
treemap.put( 4 , " D " );
treemap.put( 6 , " E " );
treemap.put( 7 , " F " );
System.out.println( "Ceiling key entry for 5: "
+ treemap.ceilingKey( 5 ));
}
}
|
Output:
Ceiling key entry for 5: 6
Program 3: To demonstrate use of ceilingKey() method when it will return null
import java.util.*;
public class Main {
public static void main(String[] args)
{
NavigableMap<Integer, String>
treemap = new TreeMap<Integer, String>();
treemap.put( 1 , " A " );
treemap.put( 2 , " B " );
treemap.put( 3 , " C " );
treemap.put( 4 , " E " );
treemap.put( 5 , " D " );
System.out.println( "Ceiling key entry for 10: "
+ treemap.ceilingKey( 10 ));
}
}
|
Output:
Ceiling key entry for 10: null
Program 4: To show NullPointerException
import java.util.*;
public class Main {
public static void main(String[] args)
{
TreeMap<Integer, String>
treemap = new TreeMap<Integer, String>();
treemap.put( 2 , " two " );
treemap.put( 1 , " one " );
treemap.put( 3 , " three " );
treemap.put( 6 , " six " );
treemap.put( 5 , " five " );
try {
System.out.println( "Ceiling key entry for null value : "
+ treemap.ceilingKey( null ));
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.NullPointerException
Program 5: To demonstrate ClassCastException
import java.util.*;
public class Main {
public static void main(String[] args)
{
NavigableMap<Object, String>
treemap = new TreeMap<Object, String>();
treemap.put( 1 , " A " );
treemap.put( 2 , " B " );
treemap.put( 3 , " C " );
treemap.put( 4 , " E " );
treemap.put( 5 , " D " );
try {
System.out.println( "Ceiling key entry for \"asd\": "
+ treemap.ceilingKey( new String( "asd" )));
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
Exception: java.lang.ClassCastException:
java.lang.Integer cannot be cast to java.lang.String
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...