Creating Subsets and Headsets from TreeMap with Java Last Updated : 06 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A Java TreeMap is a sorted collection that is a component of the Java Collections Framework. Subsets are parts of the map, and headsets are made up of items that are less than or equal to a specified key. In this article, we will learn how to create subsets and headsets from a TreeMap based on specific criteria in Java. How to Create Subsets and Headsets from a TreeMap Based on Specific Criteria in Java?Approaches to create subsets and headsets from a TreeMap SubMap(): TreeMap's subMap(K fromKey, K toKey) function yields a view of the area of the map whose keys fall between fromKey and toKey.HeadMap(): The part of the map whose keys are strictly smaller than toKey is shown in the view returned by the TreeMap headMap(K toKey) function.1. Creating a Subset using subMap()Below is the implementation of Creating a Subset using subMap() : Java // Java program to create subsets from a TreeMap import java.util.TreeMap; public class SubSetExample { public static void main(String[] args) { TreeMap<Integer, String> treeMap = new TreeMap<>(); treeMap.put(1, "A"); treeMap.put(2, "B"); treeMap.put(3, "C"); treeMap.put(4, "D"); treeMap.put(5, "E"); // creating a subset from key 3 (inclusive) to key 5 (exclusive) TreeMap<Integer, String> subMap = new TreeMap<>(treeMap.subMap(3, 5)); System.out.println("Original TreeMap: " + treeMap); System.out.println("Subset (inclusive of key 3, exclusive of key 5): " + subMap); } } OutputOriginal TreeMap: {1=A, 2=B, 3=C, 4=D, 5=E} Subset (inclusive of key 3, exclusive of key 5): {3=C, 4=D} Explanation of the Program:In the above program, a TreeMap is created.A subset is created from the original TreeMap using the subMap() method. This specifies the start (inclusive) and end (exclusive) keys.The original TreeMap and the subset are printed to the console.2. Creating a Headset using headMapBelow is the implementation of Creating a Headset using headMap: Java // Java program to create headsets from a TreeMap import java.util.TreeMap; public class HeadSetExample { public static void main(String[] args) { TreeMap<Integer, String> treeMap = new TreeMap<>(); treeMap.put(1, "A"); treeMap.put(2, "B"); treeMap.put(3, "C"); treeMap.put(4, "D"); treeMap.put(5, "E"); // Creating a headset up to key 4 (exclusive) TreeMap<Integer, String> headMap = new TreeMap<>(treeMap.headMap(4)); System.out.println("Original TreeMap: " + treeMap); System.out.println("Headset (up to key 4, exclusive): " + headMap); } } OutputOriginal TreeMap: {1=A, 2=B, 3=C, 4=D, 5=E} Headset (up to key 4, exclusive): {1=A, 2=B, 3=C} Explanation of the Program:In the above program, a TreeMap is created.A headset is created from the original TreeMap using the headMap() method, specifying the end key (exclusive).The original TreeMap and the headset are printed to the console. Comment More infoAdvertise with us Next Article Getting submap, headmap, and tailmap from Java TreeMap R rahul709392 Follow Improve Article Tags : Java Java Programs java-TreeMap Java Examples Practice Tags : Java Similar Reads Getting submap, headmap, and tailmap from Java TreeMap The TreeMap is used to implement Map interface and Navigable Map with the AbstractMap class in Java. Various constructors can be used in the TreeMap by to maintain the sorted ordering of its keys. A. subMap() method of TreeMap The subMap() method in Java is used to return the part or portion of the 8 min read Creating HashMap from Other Maps in Java Map interface present in java.util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore, it behaves a bit differently from the rest of the collection types. A map contains unique keys. There are three main types of maps in 3 min read How to Create a TreeSet with a List in Java? TreeSet is an implementation of the SortedSet interface in Java that uses a Tree for storage. TreeSet can be created from List by passing the List to the TreeSet constructor in Java or we can traverse complete List and adding each element of the List to the TreeSet. Example: Input : List = [a, b, c] 3 min read Creating and Populating a TreeSet in Java In Java, TreeSet is a pre-defined class that can be used to implement the Set interface and it is a part of the Java collection framework TreeSet is a NavigableSet implementation based on the TreeMap. TreeSet follows the natural order which means elements can be stored in sorted order and it cannot 2 min read How to Create TreeMap Objects using Comparable Interface in Java? In Java, the TreeMap class is an implementation of the SortedMap interface that stores key-value pairs in a sorted order based on the natural ordering of the keys. By default, the keys are sorted in ascending order. If you want to sort the keys based on a custom ordering criteria, you can use the Co 6 min read Creating TreeSet with Comparator by User Define Objects in Java TreeSet is the implementation class of Set Interface. It follows a natural sorting order or you can customize it using a comparator and it also does not allow duplicates. Syntax: TreeSet<String> gfg= new TreeSet<>(); Below is the normal implementation of the TreeSet: Java // Java program 6 min read Like