Best implementation for hashCode method for a collection

后端 未结 20 3437
难免孤独
难免孤独 2020-11-22 01:39

How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ?

20条回答
  •  甜味超标
    2020-11-22 02:23

    about8.blogspot.com, you said

    if equals() returns true for two objects, then hashCode() should return the same value. If equals() returns false, then hashCode() should return different values

    I cannot agree with you. If two objects have the same hashcode it doesn't have to mean that they are equal.

    If A equals B then A.hashcode must be equal to B.hascode

    but

    if A.hashcode equals B.hascode it does not mean that A must equals B

提交回复
热议问题