This Java Example shows how to get the portion of TreeMap whose keys are less than the specified key.
Get Head Map from Java TreeMap Example
This Java Example shows how to get the portion of TreeMap whose keys are less than the specified key.
To get the values from TreeMap object we use SortedMap headMap(Object toKey). This method returns
portion of TreeMap whose keys are less than specified key.
Example: headMapofTreeMap.java
package devmanuals.com; import java.util.*; public class headMapofTreeMap { public static void main(String args[]) { TreeMap Tmap = new TreeMap(); Tmap.put(1, "Singh"); Tmap.put(3, "Kumar"); Tmap.put(4, "Kumar"); Tmap.put(2, "Nagar"); Tmap.put(6, "Prakash"); SortedMap map = Tmap.headMap(3); System.out.println("The head map is as : " + map); } }
Output:
The head map is as : {1=Singh, 2=Nagar} |
[ 0 ] Comments