This example shows how to get a submap, headmap, and tailmap from the TreeMap in Java. This example also shows how to use the subMap, headMap, and tailMap methods of the TreeMap class.
How to get submap, headmap, and tailmap from the TreeMap in Java?
The TreeMap class in Java provides methods to get each type of map as given below.
How to get a submap using the subMap method?
The subMap
method of the TreeMap class returns a view of part of the TreeMap whose keys are in between the specified start and end keys.
1 |
public SortedMap<K,V> subMap(K startKey, K endKey) |
Here, the startKey is inclusive (i.e. will be included in the submap) and the endKey is exclusive (i.e. will not be included in the submap).
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 |
import java.util.Map; import java.util.TreeMap; public class TreeMapSubMapsExample { public static void main(String[] args) { TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(1, "One"); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(4, "Four"); treemapNumbers.put(5, "Five"); treemapNumbers.put(6, "Six"); treemapNumbers.put(7, "Seven"); treemapNumbers.put(8, "Eight"); /* * To get a submap from the TreeMap, use * the subMap method */ //this will return a submap having keys 4, 5, and 6 (not 7) Map<Integer, String> submap = treemapNumbers.subMap(4, 7); System.out.println( "Submap contains: " + submap); } } |
Output
1 |
Submap contains: {4=Four, 5=Five, 6=Six} |
The subMap
method throws IllegalArgumentException exception if the start key is greater than the end key parameter.
By default, the start key is inclusive while the end key is exclusive in the subMap
method. If you want to change the inclusiveness, you can use the below given overloaded subMap
method.
1 |
public NavigableMap<K,V> subMap(K startKey, boolean startInclusive, K endKey, boolean endInclusive) |
How to get a headmap using the headMap method?
The headMap
method returns a view of part of the TreeMap object whose keys are less than the specified end key.
1 |
public SortedMap<K,V> headMap(K endKey) |
The end key is exclusive i.e. will not be included in the head map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(1, "One"); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(4, "Four"); treemapNumbers.put(5, "Five"); treemapNumbers.put(6, "Six"); treemapNumbers.put(7, "Seven"); treemapNumbers.put(8, "Eight"); /* * To get a head map (i.e. a sub map having keys less than the * specified key) use the headMap method */ //this will return submap having keys 1, 2, and 3 Map<Integer, String> headmap = treemapNumbers.headMap(4); System.out.println("head map contains: " + headmap); |
Output
1 |
head map contains: {1=One, 2=Two, 3=Three} |
Here, the end key is exclusive. If you want to control that, you can use the overloaded headMap
method that has an inclusiveness parameter.
1 |
public NavigableMap<K,V> headMap(K endKey, boolean inclusive) |
How to get a tailmap using the tailMap method?
The tailMap
method returns a view of the part of the TreeMap whose keys are greater than or equal to the specified start key.
1 |
public SortedMap<K,V> tailMap(K startKey) |
The specified start key is inclusive i.e. will be included in the tail map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TreeMap<Integer, String> treemapNumbers = new TreeMap<Integer, String>(); treemapNumbers.put(1, "One"); treemapNumbers.put(2, "Two"); treemapNumbers.put(3, "Three"); treemapNumbers.put(4, "Four"); treemapNumbers.put(5, "Five"); treemapNumbers.put(6, "Six"); treemapNumbers.put(7, "Seven"); treemapNumbers.put(8, "Eight"); /* * To get a tail map (i.e. a sub map having keys greater than or equal to the * specified key) use the tailMap method */ //this will return a submap having keys 6, 7, and 8 Map<Integer, String> tailmap = treemapNumbers.tailMap(6); System.out.println("tail map contains: " + tailmap); |
Output
1 |
tail map contains: {6=Six, 7=Seven, 8=Eight} |
By default, the specified start key is included in the tail map. If you do not want that, you can use the overloaded tailMap
method and specify false as a second parameter.
1 |
public NavigableMap<K,V> tailMap(K startKey, boolean inclusive) |
Important Note:
The submaps returned by the subMap
, headMap
and tailMap
methods are backed by the original TreeMap and support all operations that are supported by the TreeMap object. Any changes you make to the submaps will be reflected in the TreeMap, and vice versa.
1 2 3 4 5 6 7 8 |
Map<Integer, String> submap = treemapNumbers.subMap(4, 7); //remove an entry from the submap submap.remove(5); //the entry will be removed from the TreeMap too System.out.println( "Submap contains: " + submap); System.out.println( "TreeMap contains: " + treemapNumbers); |
Output
1 2 |
Submap contains: {4=Four, 6=Six} TreeMap contains: {1=One, 2=Two, 3=Three, 4=Four, 6=Six, 7=Seven, 8=Eight} |
However, keep in mind that if you try to put an entry in the submap which is out of the range of the submap, the code will throw IllegalArgumentException exception.
1 2 3 4 5 |
/* * this will throw IllegalArgumentException as the * specified key 9 is out of the range of 4 to 7 */ submap.put(9, "Nine"); |
Output
1 2 |
Exception in thread "main" java.lang.IllegalArgumentException: key out of range at java.util.TreeMap$NavigableSubMap.put(Unknown Source) |
This example is a part of the TreeMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap