Java Interview Question
Use this intermediate question and answer to prepare for your next Java interview.
Which methods need to be implemented in an object for it to be used as a Key in a HashMap or HashSet? Briefly explain their use. What are the requirements on these methods for the HashMap or HashSet to work correctly?
The two methods required for correctly using any type as a Key in a HashMap or HashSet are hashCode and equals.