8:34 pm - Tuesday April 23, 2019

TreeMap example program to get lowest and highest key using firstkey() and lastkey()

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
*@author :CandidJava.com
*@descriptions : This program explain the function and use of firstKey() and lastKey() method
import java.util.TreeMap;

class GetLowestHighestKeyTreeMap {
public static void main(String[] args) {
// creating TreeMap Object
TreeMap treeMap = new TreeMap();
treeMap.put("1", "One");
treeMap.put("3", "Three");
treeMap.put("2", "Two");
treeMap.put("5", "Five");
treeMap.put("4", "Four");
// to get a Lowest value in treMap using firstKey() method
System.out.println("Lowest key Stored in Java TreeMap is : "
+ treeMap.firstKey());
// to get a Highest value in treMap using lastKey() method
System.out.println("Highest key Stored in Java TreeMap is : "
+ treeMap.lastKey());


D:\CandidJava\Util\TreeMap>javac GetLowestHighestKeyTreeMap.java

Note: GetLowestHighestKeyTreeMap.java uses unchecked or unsafe oper

Note: Recompile with -Xlint:unchecked for details.

D:\CandidJava\Util\TreeMap>java GetLowestHighestKeyTreeMap

Lowest key Stored in Java TreeMap is : 1

Highest key Stored in Java TreeMap is : 5


Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply