Saturday, August 21, 2010

Maps: NavMap.java

Courtesy of Sun Web Learning Center

 1. import java.util.*;
 2. public class NavMap {
 3.   public static void main(String[] args) {
 4.     TreeMap<String, Integer> myMap = new TreeMap<String, Integer>();
 5.     myMap.put("ak", 50);
 6.     myMap.put("co", 60);
 7.     myMap.put("ca", 70);
 8.     myMap.put("ar", 80);
 9.     NavigableMap<String, Integer> myMap2 = myMap.headMap("b", true);
10.     System.out.println(myMap2.get("ak") + " " + myMap2.get("co"));
11.   }
12. }

What is the result?


A     50 60
B     50 null
C     null 60
D     null null
E     Compilation fails.
F     An exception is thrown at runtime.

B is correct. The headMap() method returns the portion of the map whose keys are less than the key sent to it.

No comments:

Post a Comment