• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Hashing  RSS feed

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following class, which are correct implementations of the hashCode() method?



The options are:
a) return 0;
b) return a;
c) return a+b;
d) return a-b;
e) return a?b; (Note: ? means that small upside down 'v' thingy -- can't find it on the keyboard)
f) return (a<<16) | b.

The answer is a,c,e.
My question is:

i was under th impression that as long as the hashcode() method employed the fields uysed in the equals method then it would be ok.
So -- according to my logic 'd' and 'f' should also be ok. But they're not.
Why?

Like.. for examplke..what does 'c' have that 'd' doesn'thave?

Edited by Corey McGlone: Added CODE Tags
[ March 25, 2005: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Donald,

^ is SHIFT + 6 on my keyboard...might be the same on yours...maybe not...

Corey,

What are these "code tags" of which you speak? I guess you are using them to better format the original post (which I did not see).

Cheers,

Si.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following class, which are correct implementations of the hashCode() method?

class ValuePair {
public int a, b;
public boolean equals(Object other) {
try {
ValuePair o = (ValuePair) other;
return (a == o.a && b == o.b)
|| (a == o.b && b == o.a);
} catch (ClassCastException cce) {
return false;
}
}
public int hashCode() {
// Provide implementation here.
}
}

The options are:
a) return 0;
b) return a;
c) return a+b;
d) return a-b;
e) return a?b; (Note: ? means that small upside down 'v' thingy -- can't find it on the keyboard)
f) return (a<<16) | b.

The answer is a,c,e.
My question is:

i was under th impression that as long as the hashcode() method employed the fields uysed in the equals method then it would be ok.
So -- according to my logic 'd' and 'f' should also be ok. But they're not.
Why?

Like.. for examplke..what does 'c' have that 'd' doesn'thave?

In the equals method,
The comparison doesn't take into consideration the order in which a, b are used.
The hashCode methods in c, e would return the same value if the order of the operands was changed.
The same can't be said about d, f as
d) (a-b) and (b-a)
f) (a<<16) | b and (b<<16) | a
would yield different results.

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, keep in mind that the equals method only checks to see if a given ValuePair object contains the same two values. It doesn't matter if they're stored in the same order. So, this code should result in two ValuePair objects that are logically equal:



The contract of the HashCode method is as follows:



Now, let's look at the solutions and see which one's conform to all these rules and which don't.

- Solution A will always be a valid hash code method. It's the most inefficient one you can create, but it meets all 3 criteria.

- Solution B isn't a valid hashcode method in this case. While those two objects are equal, invoking hashCode on "one" would result in a 5 while invoking hashCode on "two" would result in a 6. That violates rule 2.

- Solution C works just fine because addition is commutative. That means that you can change the order of the operands without impacting the result. 5 + 6 = 11. 6 + 5 = 11. Therefore, invoking that hashCode method on either object will return the same value - this hashCode is valid.

- Solution D doesn't work because, unlike addition, subtraction is not commutative. 5 - 6 = -1. 6 - 5 = 1. The hashCode methods would return different results for logically equivalent objects. That's a no-no.

- Solution E works because the XOR operation (^ - which is also Shift-6) is commutative, just as addition is.

- Solution F fails because, like subtraction, the operation is not commutative. Try it for yourself and see.

I hope that helps.

Edit: By the way, I wrote an article about Hash Codes here, in case you're interested.
[ March 25, 2005: Message edited by: Corey McGlone ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Simon Cockayne:

What are these "code tags" of which you speak? I guess you are using them to better format the original post (which I did not see).



When posting code, put them within UBB Code Tags in order to retain whitespace and formatting. It makes it a lot easier to read. If you don't want to memorize the syntax, just use the "Instant UBB Code" buttons beneath the post window when generating a reply.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool Corey. Thanks.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!