Get Head Map from Java TreeMap Example

Get Head Map from Java TreeMap Example


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

This Java Example shows how to get the portion of TreeMap whose keys are less than the specified key.

Get Head Map from Java TreeMap Example

This Java Example shows how to get the portion of TreeMap whose keys are less than the specified key. To get the values from TreeMap object we use SortedMap headMap(Object toKey). This method returns portion of TreeMap whose keys are less than specified key.

 Example: headMapofTreeMap.java

package devmanuals.com;

import java.util.*;

public class headMapofTreeMap {
	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");
		SortedMap map = Tmap.headMap(3);
		System.out.println("The head map is as : " + map);
	}
}

Output:

The head map is as : {1=Singh, 2=Nagar}

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics