두 객체가 동일한 hashCode를 가지면 Equals()가 참이어야 합니다, 그렇죠?

bbangho·2023년 9월 11일
0

java

목록 보기
4/12

아니다!

If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

자바 공식문서에 있는 내용이다.

만약 equlas()를 통해 같은 객체라고 판단되면 반드시 같은 hashCode값을 반환해야한다고 설명한다.

equlas()가 true라면 hashcode도 같아야하지만 hashcode가 같다고 equals()가 true일 필요는 없다.

However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

밑에는 주의사항을 설명해놓았다.

참고:
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()

https://stackoverflow.com/questions/17027777/relationship-between-hashcode-and-equals-method-in-java

profile
2024. 06.17

0개의 댓글