calling containsKey on a hashmap with custom class

前端 未结 1 1334
后悔当初
后悔当初 2020-12-31 18:18

I have a Color class that I\'m putting in the hashmap. I\'d like to call containsKey on the hashmap to ensure whether the object is already present in the hashm

相关标签:
1条回答
  • 2020-12-31 18:34

    Your custom class Color should override equals() and hashcode() methods to achieve what you want.

    When you are using custom objects as keys for collections and would like to do lookup using object, then you should properly override equals() and hashcode() methods.

    Also read:

    Overriding equals and hashCode in Java

    0 讨论(0)
提交回复
热议问题