Given:
1. import java.util.*; 2. public class Main { 3. public static void main(String[] args) { 4. Map<String, String> hm = new HashMap<String, String>(); 5. String[] k = {null, "2", "3", null, "5"}; 6. String[] v = {"a", "b", "c", "d", "e"}; 7. // w ww . ja v a2 s. c om 8. for(int i=0; i<5; i++) { 9. hm.put(k[i], v[i]); 10. System.out.print(hm.get(k[i]) + " "); 11. } 12. System.out.print(hm.size() + " " + hm.values() + "\n"); 13. } }
What result is most likely?
C is correct.
It's legal for a HashMap to have one null key, and if you invoke put()
using an existing key, the new value replaces the old value.
The values()
method does NOT guarantee any ordering.