SortedMap headMap() method in Java Last Updated : 30 Sep, 2018 Comments Improve Suggest changes Like Article Like Report The headMap() method of SortedMap interface in Java is used to return a view of the portion of this map whose keys are strictly less than toKey. The map returned by this method is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The map returned by this method supports all optional map operations that this map supports. Note: The map returned by this method will throw an IllegalArgumentException if any attempt is made to insert a key outside its range. Syntax: SortedMap<K, V> headMap(K toKey) Where, K is the type of key maintained by this Set and V is the type of values associated with the Key. Parameters: This function accepts a single parameter toKey which represents high endpoint (exclusive) of the keys in the returned map. Return Value: It returns a view of the portion of this map whose keys are strictly less than toKey. Exception: ClassCastException: If the parameter toKey is not compatible with this map's comparator (or, if the map has no comparator, if toKey does not implement Comparable). NullPointerException: If the parameter toKey is null and this map does not permit null keys. IllegalArgumentException: If this map itself has a restricted range, and toKey lies outside the bounds of the range Below programs illustrate the above method: Program 1: Java // A Java program to demonstrate // working of SortedSet import java.util.*; public class Main { public static void main(String[] args) { // Create a TreeSet and inserting elements SortedMap<Integer, String> mp = new TreeMap<>(); // Adding Element to SortedSet mp.put(1, "One"); mp.put(2, "Two"); mp.put(3, "Three"); mp.put(4, "Four"); mp.put(5, "Five"); // Returning the map with key less than 3 System.out.print("Last Key in the map is : " + mp.headMap(3)); } } Output: Last Key in the map is : {1=One, 2=Two} Program 2: Java // A Java program to demonstrate // working of SortedSet import java.util.*; public class Main { public static void main(String[] args) { // Create a TreeSet and inserting elements SortedMap<String, String> mp = new TreeMap<>(); // Adding Element to SortedSet mp.put("One", "Geeks"); mp.put("Two", "For"); mp.put("Three", "Geeks"); mp.put("Four", "Code"); mp.put("Five", "It"); // Returning map with key less than H System.out.print("Last Key in the map is : " + mp.headMap("H")); } } Output: Last Key in the map is : {Five=It, Four=Code} Reference: https://docs.oracle.com/javase/10/docs/api/java/util/SortedMap.html#headMap(K) Comment More infoAdvertise with us Next Article SortedMap headMap() method in Java B barykrg Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads TreeMap headMap() Method in Java The java.util.TreeMap.headMap(key_point) method of TreeMap class is used to get all the pairs or portion of the map strictly less than the parameter key_value. The mentioned parameter is excluded from the newly prepared treemap. Since the set is backed by the map, so any changes to the map are refle 3 min read SortedSet headSet() method in Java The headSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements are strictly less than the parameter toElement. The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The 2 min read SortedMap firstKey() method in Java The firstKey() method of SortedMap interface in Java is used to return the first or the lowest key currently in this map. Syntax: K firstKey() Where, K is the type of key maintained by this Set. Parameters: This function does not accepts any parameter. Return Value: It returns the first print the lo 2 min read SortedMap put() method in Java with Examples The put() method of SortedMap interface in Java is used to associate the specified value with the specified key in this map. Syntax: V put(K key, V value) Parameters: This method has two arguments: key: which is the left argument and value: which is the corresponding value of the key in the map. Ret 2 min read SortedMap get() method in Java with Examples The get() method of SortedMap interface in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. Syntax: thisMap.get(Object key_element) Parameter: The method takes one parameter key_eleme 2 min read Like