Get SubMap in Java SortedMap Example

Get SubMap in Java SortedMap Example


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

This Java Example shows how to Get sub map of Java SortedMap.

Get SubMap in Java SortedMap Example

This Java Example shows how to Get sub map of Java SortedMap. To get sub map of SortedMap We use SortedMap subMap(K fromKey, K toKey)  method. This method returns sub map from SortedMap object containing the entries between the specifies key values. 

 Example: SubMapFromSortedMap.java

package devmanuals.com;

import java.util.*;

public class SubMapFromSortedMap {
	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 submap = Smap.subMap(2, 5);
		System.out.println("The Sorted map elements are : " + Smap);
		System.out.println("The Sub Map from sortedMap is as : " + submap);

	}
}

Output:

The Sorted map elements are : 

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

 The Sub Map from sortedMap is as : 

{2=Nagar, 3=Kumar, 4=Kumar}

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics