Java TreeMap pollLastEntry() Method
Example
Remove the last entry of a sorted map:
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
TreeMap<String, String> capitalCities = new TreeMap<String, String>();
capitalCities.put("England", "London");
capitalCities.put("Austria", "Wien");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities.pollLastEntry());
System.out.println(capitalCities);
}
}
Definition and Usage
The pollLastEntry() method removes the entry (a key/value pair) with the last (highest) key from the map, and returns it.
If the map is empty, the method returns null.
Tip: Use the lastEntry() method to get the last entry without removing it.
Syntax
public Map.Entry<K,V> pollLastEntry()
K refers to the data type of the keys in the map, and V refers to the data type of the values.
Parameter Values
None.
Technical Details
| Returns: | The removed entry, or null if the map is empty. |
|---|
Related Pages
❮ TreeMap Methods