[Java] hashCode에 대한 나름의 이해

2024. 1. 21. 17:25Java

java.lang package에 있는 Object Class의 메소드 중 하나인 hashCode는 다른 Object에 대해서는 다른 integers을 리턴해주어야 한다. 

equals method에 의하여 값이 같으면 hashCode도 같은 값을 리턴해야 한다.

equals method에 의하여 값이 다르면 hashCode는 다른 값을 리턴해야 하는 의무는 없으나 다른 값을 리턴하는게 권장된다.

 

Reference


https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#hashCode()

 

Object (Java SE 21 & JDK 21)

java.lang.Object public class Object Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. Since: 1.0 See Also: Constructor Summary Constructors Method S

docs.oracle.com