Get Head Map from Java SortedMap Example

Get Head Map from Java SortedMap Example


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

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

Get Head Map from Java SortedMap Example

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

 Example: headMapofSortedMap.java

package devmanuals.com;

import java.util.*;

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

Output:

The Map elements are as : 

{1=Singh, 2=Nagar, 3=Kumar, 4=Kumar, 6=Prakash} 

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