Given:
2. import java.util.*; 3. public class Main { 4. public static void main(String[] args) { 5. Map<Employee, String> hm = new HashMap<Employee, String>(); 6. hm.put(new Employee("Charis"), "Summer 2009"); 7. hm.put(new Employee("Jack"), "Spring 2002"); 8. Employee f = new Employee(args[0]); 9. System.out.println(hm.get(f)); 10. } /*from ww w . ja va 2 s. c om*/ 11. } 12. class Employee { 13. String name; 14. Employee(String n) { name = n; } 15. }
And the command line invocation:
java Main Jack
What is the result?
A is correct.
The Employee class doesn't override equals()
and hashCode()
, so the key to the HashMap is a specific instance of Employee, not the value of a given Employee instance's name.