1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/* Get lowest and highest key stored in Java TreeMap example This Java Example shows how to get the lowest and highest key stored in the java TreeMap object using firstKey and lastKey methods of java TreeMap class. */ import java.util.TreeMap; public class GetLowestHighestKeyTreeMapExample { public static void main(String[] args) { //create TreeMap object TreeMap treeMap = new TreeMap(); //add key value pairs to TreeMap treeMap.put("1","One"); treeMap.put("3","Three"); treeMap.put("2","Two"); treeMap.put("5","Five"); treeMap.put("4","Four"); /* To get the lowest key currently stored in Java TreeMap use, Object firstKey() method of TreeMap class. This method returns the first or lowest key currently stored in the TreeMap object. */ System.out.println("Lowest key Stored in Java TreeMap is : " + treeMap.firstKey()); /* To get the highest key currently stored in Java TreeMap use, Object lastKey() method of TreeMap class. This method returns the last or highest key currently stored in the TreeMap object. */ System.out.println("Highest key Stored in Java TreeMap is : " + treeMap.lastKey()); } } /* Output would be Lowest key Stored in Java TreeMap is : 1 Highest key Stored in Java TreeMap is : 5 */ |
gives error when key has values 1 to 10 and ,
for which answer for highest value is 9 not 10 .