🎴 Flashcard Mode
programming languages Online Quiz - 319
Given: 11. public class Key { 12. private long id1; 13. private long 1d2; 14. 15. // class Key methods 16. } A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap. Which two methods should be overridden to assure that Key works correctly as a key? (Choose two.)
For a class to work correctly as a HashMap key, it must override hashCode() and equals(Object). HashMap uses hashCode() to bucket entries and equals() to compare keys. The equals() method signature must be 'public boolean equals(Object o)' - overriding Object.equals() requires the Object parameter type, not Key. Option B's 'equals(Key k)' would be overloading, not overriding. Options A and D are correct - hashCode() and equals(Object) must both be overridden consistently.