# equals() and hashcode() from K&B

Paulo Aquino

Ranch Hand

Posts: 202

posted 10 years ago

- 0

Given the following,

11. x = 0;

12. if (x1.hashCode() != x2.hashCode() ) x = x + 1;

13. if (x3.equals(x4) ) x = x + 10;

14. if (!x5.equals(x6) ) x = x + 100;

15. if (x7.hashCode() == x8.hashCode() ) x = x + 1000;

16. System.out.println("x = " + x);

and assuming that the equals () and hashCode() methods are property implemented and x1 �

x8 are all of the same type, if the output is �x = 1111�, which of the following statements

will always be true?

A. x2.equals(x1)

B. x3.hashCode() == x4.hashCode()

C. x5.hashCode() != x6.hashCode()

D. x8.equals(x7)

The answer was...

1. B. By contract, if two objects are equivalent according to the equals() method, then

the hashCode() method must evaluate them to be ==.

A is incorrect because if the hashCode() values are not equal, the two objects must not

be equal. C is incorrect because if equals() is not true there is no guarantee of any result

from hashCode(). D is incorrect because hashCode() will often return == even if the

two objects do not evaluate to equals() being true.

I can't undertand why the answer given was like this.

11. x = 0;

12. if (x1.hashCode() != x2.hashCode() ) x = x + 1;

13. if (x3.equals(x4) ) x = x + 10;

14. if (!x5.equals(x6) ) x = x + 100;

15. if (x7.hashCode() == x8.hashCode() ) x = x + 1000;

16. System.out.println("x = " + x);

and assuming that the equals () and hashCode() methods are property implemented and x1 �

x8 are all of the same type, if the output is �x = 1111�, which of the following statements

will always be true?

A. x2.equals(x1)

B. x3.hashCode() == x4.hashCode()

C. x5.hashCode() != x6.hashCode()

D. x8.equals(x7)

The answer was...

1. B. By contract, if two objects are equivalent according to the equals() method, then

the hashCode() method must evaluate them to be ==.

A is incorrect because if the hashCode() values are not equal, the two objects must not

be equal. C is incorrect because if equals() is not true there is no guarantee of any result

from hashCode(). D is incorrect because hashCode() will often return == even if the

two objects do not evaluate to equals() being true.

I can't undertand why the answer given was like this.

Be Afraid...Be very Afraid...

Ram Naresh

Greenhorn

Posts: 11

posted 10 years ago

- 0

Hi Paulo,

The reason is because if the objects are equal by equals(), they will always return the same hashcode. But if the objects are not equal by equals() it doesn't mean that they may return different hashcode.Another thing which can be concluded is that if the hashcodes are not same they will never be equal.Hope this clarifies your doubt.

Ram.

The reason is because if the objects are equal by equals(), they will always return the same hashcode. But if the objects are not equal by equals() it doesn't mean that they may return different hashcode.Another thing which can be concluded is that if the hashcodes are not same they will never be equal.Hope this clarifies your doubt.

Ram.

sai Venka

Greenhorn

Posts: 9

posted 10 years ago

- 0

Consider we have 3 set of different color balls.each set contains 3 balls.

Red-3,blue-3,white-3.so totally we have 9 balls.Consider these 9 balls differ in weight.i want to store these balls in somewhere and whenever i require the particular weight ball,i need to fetch the correct ball.

so instead of putting all the balls in one bucket,if we store these balls in different bucket,it will be easy for us to find and fetch the correct ball with minimum search time.so i am going to store these 3 different color balls in 3 different buckets.

RED bucket contains-red balls

BLUE bucket contains-blue balls

WHITE bucket contains-white balls

if my requirement is that fetch 5kg red ball then, first i need to find the right bucket and search each ball which one is 5kg in weight.

it is easy for us to search only 3 balls in RED bucket.if we stored our 9 balls in one bucket,we would have searched long time to get the right ball.

so,in java perspective,consider Hashcode determines in which bucket the value has to be stored and from which bucket the value has to be retrieved.Here,the bucket may contain many values(like RED bucket contain 3 red balls).but all the values have same hascode(all the red balls stored in RED bucket) but they may differ(like RED bucket balls differ in weight) someway.

Analyze your question now,you can get the answer.

Red-3,blue-3,white-3.so totally we have 9 balls.Consider these 9 balls differ in weight.i want to store these balls in somewhere and whenever i require the particular weight ball,i need to fetch the correct ball.

so instead of putting all the balls in one bucket,if we store these balls in different bucket,it will be easy for us to find and fetch the correct ball with minimum search time.so i am going to store these 3 different color balls in 3 different buckets.

RED bucket contains-red balls

BLUE bucket contains-blue balls

WHITE bucket contains-white balls

if my requirement is that fetch 5kg red ball then, first i need to find the right bucket and search each ball which one is 5kg in weight.

it is easy for us to search only 3 balls in RED bucket.if we stored our 9 balls in one bucket,we would have searched long time to get the right ball.

so,in java perspective,consider Hashcode determines in which bucket the value has to be stored and from which bucket the value has to be retrieved.Here,the bucket may contain many values(like RED bucket contain 3 red balls).but all the values have same hascode(all the red balls stored in RED bucket) but they may differ(like RED bucket balls differ in weight) someway.

Analyze your question now,you can get the answer.

I agree. Here's the link: http://aspose.com/file-tools |