- 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
Java Program to get highest key stored in TreeMap
Use the lastKey() method to get the highest key stored in TreeMap.
Let us first set the TreeMap and add some elements to it −
TreeMap<Integer,String> m = new TreeMap<Integer,String>(); m.put(1,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS");
Now get the highest key −
m.lastKey()
The following is an example to get the highest key in 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,"PHP"); m.put(2,"jQuery"); m.put(3,"JavaScript"); m.put(4,"Ruby"); m.put(5,"Java"); m.put(6,"AngularJS"); m.put(7,"ExpressJS"); System.out.println("TreeMap Elements...
"+m); System.out.println("Highest key in TreeMap: " + m.lastKey()); } }
Output
TreeMap Elements... {1=PHP, 2=jQuery, 3=JavaScript, 4=Ruby, 5=Java, 6=AngularJS, 7=ExpressJS} Highest key in TreeMap: 7
- Related Articles
- Get lowest key stored in Java TreeMap
- Java Program to check if a particular key exists in TreeMap
- Java Program to get Tail Map from TreeMap
- Java Program to get Sub Map from TreeMap
- Remove a key from TreeMap in Java
- Java Program to get the lowest and highest value in TreeSet
- Get Sub Map from TreeMap in Java
- Get Tail Map from TreeMap in Java
- Get Head Map from TreeMap in Java
- Program to convert HashMap to TreeMap in Java
- Create a TreeMap in Java and add key-value pairs
- Java Program to convert HashMap to TreeMap
- Java Program to Get key from HashMap using the value
- Java Program to remove a key from a TreeMap only if it is associated with a given value
- Java Program to check if a particular value exists in TreeMap

Advertisements