- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove the last entry of the TreeMap in Java
To remove the last entry of the TreeMap, use the pollLastEntry() method.
Let us first create a TreeMap and add elements
TreeMap<Integer, String> m = new TreeMap<Integer, String>(); m.put(1,"India"); m.put(2,"US"); m.put(3,"Australia"); m.put(4,"Netherlands"); m.put(5,"Canada");
Remove the last entry now
m.pollLastEntry()
The following is an example to remove the last entry of the TreeMap
Example
import java.util.*; public class Demo { public static void main(String args[]){ TreeMap<Integer, String> m = new TreeMap<Integer, String>(); m.put(1,"India"); m.put(2,"US"); m.put(3,"Australia"); m.put(4,"Netherlands"); m.put(5,"Canada"); System.out.println("TreeMap Elements = "+m); System.out.println("Removing Last Entry : "+m.pollLastEntry()); System.out.println("Updated TreeMap Elements = "+m); } }
Output
The following is the output
TreeMap Elements = {1=India, 2=US, 3=Australia, 4=Netherlands, 5=Canada} Removing Last Entry : 5=Canada Updated TreeMap Elements = {1=India, 2=US, 3=Australia, 4=Netherlands}
- Related Articles
- Remove the first entry of the TreeMap in Java
- Retrieve the last entry in the TreeMap in Java
- Retrieve the first entry in the TreeMap in Java
- Remove all values from TreeMap in Java
- Remove a value from TreeMap in Java
- Remove a key from TreeMap in Java
- Get last entry from NavigableMap in Java
- Iterate through the values of TreeMap in Java
- How to remove the last character from a string in Java?
- Updating last entry of a particular ID in MySQL
- Get Last Entry in a MySQL table?
- Remove entry with specified key from the StringDictionary in C#
- Remove the entry at specified index from OrderedDictionary in C#
- Remove the entry with specified key from ListDictionary in C#
- SELECT last entry without using LIMIT in MySQL?

Advertisements