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} |
[ 0 ] Comments