TreeMap head sub map
In this chapter you will learn:
Head sub Map
SortedMap<K,V> headMap(K toKey)
Returns a view of the portion of this map whose keys are strictly less than toKey.
import java.util.SortedMap;
import java.util.TreeMap;
//from j a va 2s. c o m
public class Main {
public static void main(String args[]) {
TreeMap map = new TreeMap();
map.put("Virginia", "Richmond");
map.put("Massachusetts", "Boston");
map.put("New York", "Albany");
map.put("Maryland", "Annapolis");
SortedMap m = map.headMap("New York");
System.out.println(m);
}
}
The output:
NavigableMap<K,V> headMap(K toKey, boolean inclusive)
Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
import java.util.SortedMap;
import java.util.TreeMap;
// j a v a2s . c om
public class Main {
public static void main(String args[]) {
TreeMap map = new TreeMap();
map.put("Virginia", "Richmond");
map.put("Massachusetts", "Boston");
map.put("New York", "Albany");
map.put("Maryland", "Annapolis");
SortedMap m = map.headMap("New York",true);
System.out.println(m);
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Collections