The TreeMap.pollLastEntry()
method in Java is used to retrieve and remove the last (highest) key-value pair from a TreeMap
. 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.pollLastEntry()
can be used effectively.
Table of Contents
- Introduction
pollLastEntry
Method Syntax- Examples
- Removing the Last Entry from a TreeMap
- Handling an Empty TreeMap
- Real-World Use Case
- Example: Managing a Priority Queue of Tasks
- Conclusion
Introduction
The TreeMap.pollLastEntry()
method is a member of the TreeMap
class in Java. It retrieves and removes the last (highest) key-value pair from the TreeMap
. If the map is empty, it returns null
. This method is useful when you need to process and remove the highest entry from a sorted map.
pollLastEntry() Method Syntax
The syntax for the pollLastEntry
method is as follows:
public Map.Entry<K,V> pollLastEntry()
- The method does not take any parameters.
- The method returns a
Map.Entry<K,V>
representing the last (highest) key-value pair, ornull
if the map is empty.
Examples
Removing the Last Entry from a TreeMap
The pollLastEntry
method can be used to remove the last key-value pair from a TreeMap
.
Example
import java.util.TreeMap;
import java.util.Map;
public class PollLastEntryExample {
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);
// Removing the last entry from the TreeMap
Map.Entry<String, Integer> lastEntry = treeMap.pollLastEntry();
// Printing the removed entry
System.out.println("Removed last entry: " + lastEntry);
// Printing the TreeMap after removal
System.out.println("TreeMap after removing last entry: " + treeMap);
}
}
Output:
Removed last entry: Suresh=40
TreeMap after removing last entry: {Anita=28, Priya=30, Ravi=25, Vijay=35}
Handling an Empty TreeMap
The pollLastEntry
method returns null
if the TreeMap
is empty.
Example
import java.util.TreeMap;
import java.util.Map;
public class EmptyTreeMapExample {
public static void main(String[] args) {
// Creating an empty TreeMap
TreeMap<String, Integer> treeMap = new TreeMap<>();
// Attempting to remove the last entry from an empty TreeMap
Map.Entry<String, Integer> lastEntry = treeMap.pollLastEntry();
// Printing the result
System.out.println("Removed last entry from empty TreeMap: " + lastEntry);
}
}
Output:
Removed last entry from empty TreeMap: null
Real-World Use Case
Example: Managing a Priority Queue of Tasks
A common real-world use case for TreeMap.pollLastEntry()
is managing a priority queue of tasks where the highest priority task is processed and removed.
Example
import java.util.TreeMap;
import java.util.Map;
public class TaskManager {
public static void main(String[] args) {
// Creating a TreeMap to manage tasks with priorities
TreeMap<Integer, String> taskQueue = new TreeMap<>();
// Adding tasks to the TreeMap with their priorities
taskQueue.put(1, "Complete report");
taskQueue.put(3, "Schedule meeting");
taskQueue.put(2, "Review code");
taskQueue.put(5, "Fix bugs");
taskQueue.put(4, "Update documentation");
// Processing and removing the highest priority task
Map.Entry<Integer, String> highestPriorityTask = taskQueue.pollLastEntry();
// Printing the highest priority task
System.out.println("Processed highest priority task: " + highestPriorityTask);
// Printing the remaining tasks
System.out.println("Remaining tasks: " + taskQueue);
}
}
Output:
Processed highest priority task: 5=Fix bugs
Remaining tasks: {1=Complete report, 2=Review code, 3=Schedule meeting, 4=Update documentation}
In this example, TreeMap.pollLastEntry()
is used to process and remove the highest priority task from a priority queue, making it easy to manage and update the task list.
Conclusion
The TreeMap.pollLastEntry()
method in Java provides a way to retrieve and remove the last (highest) key-value pair from a TreeMap
. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications. The method allows you to process and remove the highest entries, making it a versatile tool for data management in various scenarios.