This Java Example shows how to Get Highest and Lowest Key of Java SortedMap Map object.
Get Highest and Lowest Key of Java SortedMap Example
This Java Example shows how to Get Highest and Lowest Key of Java SortedMap
Map object. To get an key at first and last position of SortedMap We use Object
firstKey( ) and Object lastKey( ) method. These method returns first or
lowest key and last or highest key from Map object respectively.
Example: FirstAndLastKeySortedMap.java
package devmanuals.com; import java.util.*; public class FirstAndLastKeySortedMap { public static void main(String args[]) { SortedMap Smap = new TreeMap(); Smap.put(1, "Singh"); Smap.put(3, "Kumar"); Smap.put(4, "Kumar"); Smap.put(2, "Nagar"); Smap.put(6, "Prakash"); System.out.println("The elements of Sorted Map is : " + Smap); System.out .println("The First or lowest key is as : " + Smap.firstKey()); System.out.println("The Last or Highest key is as : " + Smap.lastKey()); } }
Output:
The elements of Sorted Map is :
{1=Singh, 2=Nagar, 3=Kumar, 4=Kumar, 6=Prakash} The First or lowest key is as : 1 The Last or Highest key is as : 6 |
[ 0 ] Comments