Get Highest and Lowest Key of Java TreeMap Example

Get Highest and Lowest Key of Java TreeMap Example


Posted in : Java Posted on : December 3, 2010 at 5:16 PM Comments : [ 0 ]

This Java Example shows how to Get Highest and Lowest Key of Java TreeMap object.

Get Highest and Lowest Key of Java TreeMap Example

This Java Example shows how to Get Highest and Lowest Key of Java TreeMap object. To get an key at first and last position of TreeMap We use Object firstKey( ) and Object lastKey( ) method. These method returns first or lowest key and last or highest key from TreeMap object respectively. 

 Example: FirstAndLastKeyTreeMap.java

package devmanuals.com;

import java.util.*;

public class FirstAndLastKeyTreeMap {
	public static void main(String args[]) {
		TreeMap Tmap = new TreeMap();
		Tmap.put(1, "Singh");
		Tmap.put(3, "Kumar");
		Tmap.put(4, "Kumar");
		Tmap.put(2, "Nagar");
		Tmap.put(6, "Prakash");
		System.out
				.println("The First or lowest key is as : " + Tmap.firstKey());

		System.out.println("The Last or Highest key is as : " + Tmap.lastKey());
	}
}

Output:

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