1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/* Get Sub Map from Java TreeMap example This Java Example shows how to get the sub Map from Java Treemap by giving specific range of keys using subMap method of Java TreeMap class. */ import java.util.TreeMap; import java.util.SortedMap; public class GetSubMapFromTreeMapExample { public static void main(String[] args) { //create TreeMap object TreeMap treeMap = new TreeMap(); //add key value pairs to TreeMap treeMap.put("1","One"); treeMap.put("3","Three"); treeMap.put("2","Two"); treeMap.put("5","Five"); treeMap.put("4","Four"); /* To get the sub Map from Java TreeMap use, SortedMap subMap(int fromKey, int toKey) method of TreeMap class. This method returns portion of the TreeMap whose keys range from fromKey (inclusive) to toKey(exclusive). Please note that, the SortedMap returned by this method is backed by the original TreeMap. So any changes made to SortedMap will be reflected back to original TreeMap. */ SortedMap sortedMap = treeMap.subMap("2","5"); System.out.println("SortedMap Contains : " + sortedMap); /* Please also note that, - SortedMap throws IllegalArgumentException for any attempts to insert the key less than fromKey and grater than or equal to toKey. - subMap throws ClassCastException, if fromKey and toKey can not be compared using Map's Comparators - subMap throws IllegalArgumentException, if fromKey is grater than toKey */ } } /* Output would be SortedMap Contains : {2=Two, 3=Three, 4=Four} */ |
Add Comment