question about hashCode in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

question about hashCode in java

public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } can you explan how this code work ,and what is the benefit? ?

31st Aug 2021, 10:44 PM
Nora
Nora - avatar
1 Answer
+ 2
I'm not sure about Java but I think it's a unique value representing a unique object, there's 2 ways to define/override it, multiple it by prime numbers or use the id from the relative database: // written in C# public override int GetHashCode() { return Person?.Id; } The benefit of this is that every object will be 99% or completely unique depends on how you override it, you can then store them in a hashset, this increases the performance to search for the object
1st Sep 2021, 4:53 AM
Tim
Tim - avatar