The example aims to get a head map of TreeMap whose keys are less than the specified key by headMap method in Java.
Source Code
package com.beginner.examples;
import java.util.SortedMap;
import java.util.TreeMap;
public class GetHeadMapFromTreeMap {
public static void main(String[] args) {
//create TreeMap.
TreeMap treeMap = new TreeMap();
treeMap.put("1","Alice");
treeMap.put("2","Bob");
treeMap.put("3","Jack");
/*
* To get a Head Map from Java TreeMap using headMap method of Java TreeMap.
*/
SortedMap sortedMap = treeMap.headMap("3");
System.out.println("Head Map Contains : " + sortedMap);
}
}
Output:
Head Map Contains : {1=Alice, 2=Bob}
References
Imported packages in Java documentation: