i presume equals() also compares hashCode values, ...
# getting-started
h
i presume equals() also compares hashCode values, meaning i should only include values in the hashCode function that i depend on in the equals function?
t
By default
equals()
just compares if the object reference is the same which isn't really that useful, so you have to override
hashCode()
so that it compares if the values inside are the same or not. It's also used in Collections a lot. Most IDEs come with
equals
and
hashCode
generators. Also Kotlin's data classes implement these by default, so for those you don't need to override.
👍 1
s
hashCode() is used for efficient lookup when objects are stored in HashMap, HashSet
👍 1