Open In App

TreeMap ceilingKey() in Java with Examples

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The ceilingKey() function of the TreeMap Class returns the least key greater than or equal to the given key or null if 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 the 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.

Java Programs to illustrate the use of ceilingKey() in TreeMap

Below are the examples to illustrate ceilingKey() method:

Program 1: Using ceilingKey() with a Custom Comparator

Java
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        // Creating a TreeMap with a custom comparator
        NavigableMap<Integer, String> treemap = new TreeMap<>((a, b) -> a - b); // natural ordering

        // Adding key-value pairs to the TreeMap
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " D ");
        treemap.put(6, " E ");

        // Searching for the least key greater than or equal to 5
        System.out.println("Ceiling key entry for 5: " + treemap.ceilingKey(5));
    }
}

Output
Ceiling key entry for 5: 6

Explanation:

  • In this program, we created a TreeMap using a custom comparator, but the comparator ((a, b) -> a - b) behaves like natural ordering.
  • The ceilingKey(5) searches for the least key greater than or equal to 5.
  • Since 6 is the smallest key that satisfies this condition, it returns 6.

Program 2: Using ceilingKey() without a Custom Comparator

Java
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        // Creating a TreeMap without a custom comparator (natural ordering)
        NavigableMap<Integer, String> treemap = new TreeMap<>();

        // Adding key-value pairs to the TreeMap
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " D ");
        treemap.put(6, " E ");
        treemap.put(7, " F ");

        // Searching for the least key greater than or equal to 5
        System.out.println("Ceiling key entry for 5: " + treemap.ceilingKey(5));
    }
}

Output
Ceiling key entry for 5: 6

Explanation:

  • In this case, we are using a TreeMap without a custom comparator, meaning it uses the natural ordering of integers.
  • The ceilingKey(5) method returns 6 because it is the smallest key greater than or equal to 5.

Program 3: Using ceilingKey() when it returns null

Java
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        // Creating a TreeMap with natural ordering
        NavigableMap<Integer, String> treemap = new TreeMap<>();

        // Adding key-value pairs to the TreeMap
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " E ");
        treemap.put(5, " D ");

        // Searching for the least key greater than or equal to 10
        System.out.println("Ceiling key entry for 10: " + treemap.ceilingKey(10));
    }
}

Output
Ceiling key entry for 10: null

Explanation:

  • Here, the method ceilingKey(10) returns null because there is no key in the map that is greater than or equal to 10.
  • The largest key in the map is 5, which is less than 10.

Program 4: Demonstrating NullPointerException

Java
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        // Creating a TreeMap with natural ordering
        TreeMap<Integer, String> treemap = new TreeMap<>();

        // Adding key-value pairs to the TreeMap
        treemap.put(2, " two ");
        treemap.put(1, " one ");
        treemap.put(3, " three ");
        treemap.put(6, " six ");
        treemap.put(5, " five ");

        try {
            // This will throw a NullPointerException because null keys are not allowed
            System.out.println("Ceiling key entry for null: " + treemap.ceilingKey(null));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

Output
Exception: java.lang.NullPointerException

Explanation:

  • A NullPointerException is thrown because the TreeMap uses natural ordering, which does not permit null keys. Attempting to call ceilingKey(null) triggers this exception.

Program 5: Demonstrating ClassCastException

Java
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        // Creating a TreeMap that stores Object keys
        NavigableMap<Object, String> treemap = new TreeMap<>();

        // Adding Integer keys and values to the TreeMap
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " E ");
        treemap.put(5, " D ");

        try {
            // This will throw a ClassCastException because "asd" is a String, not an Integer
            System.out.println("Ceiling key entry for \"asd\": " + treemap.ceilingKey("asd"));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

Output
Exception: java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')

Explanation:

  • This program demonstrates a ClassCastException because the TreeMap expects keys of type Integer, but we attempt to search for a key using a String ("asd").
  • Since Integer and String cannot be compared, this causes the exception.

Conclusion

The ceilingKey() method in Java's TreeMap is useful for finding the least key greater than or equal to the provided key. However, it is important to handle exceptions like NullPointerException and ClassCastException that can arise when using this method with invalid or incompatible keys.


Next Article
Practice Tags :

Similar Reads