1import java.util.SortedMap;
2import java.util.TreeMap;
3public class TreeMapHeadMapMethodExample
4{
5 public static void main(String[] args)
6 {
7 TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
8 // map string values to integer keys
9 tm.put("mango", 65);
10 tm.put("apple", 63);
11 tm.put("grapes", 35);
12 tm.put("pineapple", 60);
13 tm.put("banana", 26);
14 System.out.println("Given TreeMap is: " + tm);
15 // create SortedMap for map head
16 SortedMap<String, Integer> sm = new TreeMap<String, Integer>();
17 sm = tm.headMap("pineapple");
18 // Getting map head
19 System.out.println("headmap is: " + sm);
20 }
21}
1import java.util.SortedMap;
2import java.util.TreeMap;
3public class TreeMapHeadMapMethodExample
4{
5 public static void main(String[] args)
6 {
7 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
8 // map string values to integer keys
9 tm.put(65, "mango");
10 tm.put(63, "apple");
11 tm.put(35, "grapes");
12 tm.put(60, "pineapple");
13 tm.put(26, "banana");
14 System.out.println("Given TreeMap is: " + tm);
15 // create SortedMap for map head
16 SortedMap<Integer, String> sm = new TreeMap<Integer, String>();
17 sm = tm.headMap(60);
18 // Getting map head
19 System.out.println("headmap is: " + sm);
20 }
21}