T
Todd
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.
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?
2. When would one use a set of criteria to determine equality that is
different from the criteria used to generate a hashCode?
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() );
}
}
Thanks,
Todd
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.
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?
2. When would one use a set of criteria to determine equality that is
different from the criteria used to generate a hashCode?
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() );
}
}
Thanks,
Todd