Get Highest and Lowest Key of Java SortedMap Example

Get Highest and Lowest Key of Java SortedMap Example


Posted in : Java Posted on : December 7, 2010 at 4:33 PM Comments : [ 0 ]

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

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics