Java TreeMap ceilingEntry() Method

The TreeMap.ceilingEntry() method in Java is used to find the least key-value pair in the map that is greater than or equal to the given key. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how TreeMap.ceilingEntry() can be used effectively.

Table of Contents

  1. Introduction
  2. ceilingEntry Method Syntax
  3. Examples
    • Finding the Ceiling Entry in a TreeMap
    • Handling Non-Existent Ceiling Entries
  4. Real-World Use Case
    • Example: Finding the Next Student Record
  5. Conclusion

Introduction

The TreeMap.ceilingEntry() method is a member of the TreeMap class in Java. It allows you to find the least key-value pair in the map that is greater than or equal to the given key. If no such key-value pair exists, the method returns null.

ceilingEntry() Method Syntax

The syntax for the ceilingEntry method is as follows:

public Map.Entry<K,V> ceilingEntry(K key)
  • Parameters:
    • key: The key to compare against.
  • Returns: The least key-value pair greater than or equal to the given key, or null if there is no such key.

Examples

Finding the Ceiling Entry in a TreeMap

The ceilingEntry method can be used to find the least key-value pair in the TreeMap that is greater than or equal to the specified key.

Example

import java.util.TreeMap;
import java.util.Map;

public class CeilingEntryExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Finding the least key-value pair greater than or equal to "Priya"
        Map.Entry<String, Integer> ceilingEntry = treeMap.ceilingEntry("Priya");

        // Printing the result
        System.out.println("Least entry greater than or equal to 'Priya': " + ceilingEntry);
    }
}

Output:

Least entry greater than or equal to 'Priya': Priya=30

Handling Non-Existent Ceiling Entries

If there is no key-value pair in the TreeMap that is greater than or equal to the specified key, the ceilingEntry method returns null.

Example

import java.util.TreeMap;
import java.util.Map;

public class NoCeilingEntryExample {
    public static void main(String[] args) {
        // Creating a TreeMap with String keys and Integer values
        TreeMap<String, Integer> treeMap = new TreeMap<>();

        // Adding entries to the TreeMap
        treeMap.put("Ravi", 25);
        treeMap.put("Priya", 30);
        treeMap.put("Vijay", 35);
        treeMap.put("Anita", 28);
        treeMap.put("Suresh", 40);

        // Attempting to find the least key-value pair greater than or equal to "Zara"
        Map.Entry<String, Integer> ceilingEntry = treeMap.ceilingEntry("Zara");

        // Printing the result
        System.out.println("Least entry greater than or equal to 'Zara': " + ceilingEntry);
    }
}

Output:

Least entry greater than or equal to 'Zara': null

Real-World Use Case

Example: Finding the Next Student Record

A common real-world use case for TreeMap.ceilingEntry() is finding the next student record in a sorted list of student names and their IDs.

Example

import java.util.TreeMap;
import java.util.Map;

public class StudentManager {
    public static void main(String[] args) {
        // Creating a TreeMap to manage student names and their IDs
        TreeMap<String, Integer> studentNames = new TreeMap<>();

        // Adding student names and their IDs to the TreeMap
        studentNames.put("Ravi", 101);
        studentNames.put("Priya", 102);
        studentNames.put("Vijay", 103);
        studentNames.put("Anita", 104);
        studentNames.put("Suresh", 105);

        // Finding the next student record after "Priya"
        Map.Entry<String, Integer> nextStudentRecord = studentNames.ceilingEntry("Priya");

        // Printing the next student record
        System.out.println("Next student record after 'Priya': " + nextStudentRecord);
    }
}

Output:

Next student record after 'Priya': Priya=102

In this example, TreeMap.ceilingEntry() is used to find the next student record in a sorted list based on their names, making it easy to navigate and manage the list.

Conclusion

The TreeMap.ceilingEntry() method in Java provides a way to find the least key-value pair in the map that is greater than or equal to the specified key. By understanding how to use this method, you can efficiently manage and navigate collections of key-value pairs in your Java applications. The method allows you to find and work with entries relative to other entries in the map, making it a versatile tool for data management in various scenarios.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top