doubts on hashcode implementation in java -


i thankful if can clear doubt suppose have class , b has reference of b b1 , b2. in class overrides equal method b1.equal(other.b1) , b2.equal(other.b2)

and override hashcode method

int result = 1; result = 17* result + ((b1== null) ? 0 : b1.hashcode()); result = 17* result + ((b2== null) ? 0 : b2.hashcode()); return result; 

is hashcode method correctly implemented? , if calling b1.hashcode() call hashcode method of class b? if yes, compulsory override equals , hashcode in class b?

thanks in advance.

the only requirement of hashcode() in java consistency. means 2 identical instances of a must return same hash code. if code this, technically good. means technically hashcode() function return 1 valid. though, bad implementation because has 100% collision rate. in order make hashcode function, should minimize collision rate.

with being said, code looks good.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -