Capgemini Engineering Interview Question

Can we add Object as a key in HashMap? If yes then how?

Interview Answer

Anonymous

Apr 4, 2021

Yes, But Class of the key Object must override both hashCode () and equals() methods. Custom class should follow the contract between hashCode() and equals().