- 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
Retrieve the last entry in the TreeMap in Java
Use the lastEntry() method in TreeMap to retrieve the last entry.
Create a TreeMap and add some 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");
Now, retrieve the last entry −
m.lastEntry()
The following is an example to retrieve last entry in 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"); for(Map.Entry e:m.entrySet()) { System.out.println(e.getKey()+" "+e.getValue()); } System.out.println("Last Entry in Map is "+m.lastEntry()); } }
Output
1 India 2 US 3 Australia 4 Netherlands 5 Canada Last Entry in Map is 5=Canada
- Related Articles
- Retrieve the first entry in the TreeMap in Java
- Remove the last entry of the TreeMap in Java
- Retrieve TreeMap size in Java
- Remove the first entry of the TreeMap in Java
- Retrieve the last element from a LinkedList in Java
- Get last entry from NavigableMap in Java
- Get Last Entry in a MySQL table?
- Iterate through the values of TreeMap in Java
- SELECT last entry without using LIMIT in MySQL?
- Updating last entry of a particular ID in MySQL
- Create NavigableMap from TreeMap in Java
- Retrieve all the keys from HashMap in Java
- What is the differences between TreeMap, HashMap and LinkedHashMap in Java?
- Get Sub Map from TreeMap in Java
- Remove all values from TreeMap in Java

Advertisements