Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
Java uses the hashCode method when you put an object into a Collection that uses hash tables, for example a Map. This enables very quick retrieval of data. The hashCode method must return an int that is somehow calculated by your program, the implementation is up to you. But it should be consistent with the equals() method. If two objects are equal, they must return the same hash code. The reverse is not required strictly, so two hashcodes can be the same, but the objects still not equal, but this really beats the purpose of hashing, as it does not help in speeding up your data structures. Some algorithms use prime numbers for the calculation of a unique hash. https://www.baeldung.com/java-hashcode
19th Mar 2020, 8:33 AM
Tibor Santa
Tibor Santa - avatar