Re: hashCode and equals (again)
On Wed, 9 Apr 2008 07:51:02 -0700 (PDT), Todd
<todd.heidenthal@lmco.com> wrote:
Hello,
I have spent a great deal of time reading through the postings in this
group as well as tutorials/explanations on sites elsewhere (i.e.,
Roedy's, etc.), but have not been able to get a good grasp of hashCode
and equals. I understand most of the rules for hashCode are defined
for use of objects in maps and other comparable collections, so it is
from that POV that I am trying to get a good grasp of the concepts.
Please help if you can - especially the SCCE later.
Have a read of Chapter Three of "Effective Java":
http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf That
should help,
1. Originally, I thought that it made sense to make an equals method
that uses hashCode as its criteria for equality. However, as I now
understand hashCode, the code _must_ be the same for equal objects,
BUT it is _possible_ to be the same for non-equal objects. Am I
stating this correctly?
Yes. In general there will be objects that have the same hash code
but are not equal. It would be possible to use the hash code as a
filter - if the hash codes are different then the objects are not
equal, but thet depends on the correct implementation of hashCode(),
as you have noticed in your third question.
2. When would one use a set of criteria to determine equality that is
different from the criteria used to generate a hashCode?
A hash code must be an integer, which sets a limit on how many
different hash codes there are. There are far more possible strings
than there are integers/hash codes, so different strings will have to
share the same hash code.
3. Why aren't the hashCode_s in the following code the same?
package hashcode;
public class Main
{
public static void main(String[] args)
{
int[] a = { 1, 7, 0, 0 };
int[] b = { 1, 7, 0, 0 };
System.out.println( "equals: " + a.equals( b ) );
System.out.println( "hash a: " + a.hashCode() );
System.out.println( "hash b: " + b.hashCode() );
Integer[] c = { 1, 7, 0, 0 };
Integer[] d = { 1, 7, 0, 0 };
System.out.println( "equals: " + c.equals( d ) );
System.out.println( "hash c: " + c.hashCode() );
System.out.println( "hash d: " + d.hashCode() );
int[] e = a.clone();
Integer[] f = c.clone();
System.out.println( "equals: " + a.equals( e ) );
System.out.println( "hash a: " + a.hashCode() );
System.out.println( "hash e: " + e.hashCode() );
System.out.println( "equals: " + c.equals( f ) );
System.out.println( "hash c: " + c.hashCode() );
System.out.println( "hash f: " + f.hashCode() );
}
}
What results are you expecting? Why are you expecting those results?
I get:
equals: false
hash a: 25345246
hash b: 4047035
equals: false
hash c: 5324129
hash d: 26530674
equals: false
hash a: 25345246
hash e: 29752800
equals: false
hash c: 5324129
hash f: 27165481
You might also try using Arrays.equals():
equals: false
Arrays.equals: true
hash a: 27692793
hash b: 32801378
equals: false
Arrays.equals: true
hash c: 26999600
hash d: 25706868
equals: false
Arrays.equals: true
hash a: 27692793
hash e: 8470547
equals: false
Arrays.equals: true
hash c: 26999600
hash f: 26596606
rossum
Thanks,
Todd