Get SubMap in Java TreeMap Example

Get SubMap in Java TreeMap Example


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

This Java Example shows how to Get sub map of Java TreeMap object.

Get SubMap in Java TreeMap Example

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

 Example: SubMapFromTreeMap.java

package devmanuals.com;

import java.util.*;

public class SubMapFromTreeMap {
	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 submap = Tmap.subMap(2, 5);
		System.out.println("The Sub Map from TreeMap is as : " + submap);

	}
}

Output:

The Sub Map from TreeMap 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