Discussion:
hashCode()
(too old to reply)
Douglas Harder
2005-01-15 07:31:50 UTC
Permalink
Hello,

Someone after class claimed that he could use Object's default hashCode()
method without overriding it and still satisfy the conditions required by
the project specifications. I have tried, again and again, to recreate the
behaviour where multiple objects with identical instance variables created
the same hash using Object's default hashCode method. Would this individual
please supply me with the source code which produced this interesting
result? In the following piece of code, you will note that each object,
although having equal instance variables, returns different hash values.

public class Temp {
private int x;

public Temp( int i ) {
x = i;
}

public static void main( String[] args ) {
Temp[] a = new Temp[1000];

for ( int i = 0; i < a.length; i++ ) {
a[i] = new Temp( 0 );

System.out.println( a[i].hashCode() );
}

for ( int i = 0; i < 1000; i++ ) {
Temp x = new Temp( 0 );
System.out.println( x.hashCode() );
}
}
}

If you can give me something which is reproducable, I would be very
interested.

Thank you,

Douglas
Asimo
2005-01-18 22:57:11 UTC
Permalink
for hashCode, is it correct to return the same hashCode for 0/7 and 0/12

since both equate to 0, my code produces the same hashCode for it.
Post by Douglas Harder
Hello,
Someone after class claimed that he could use Object's default hashCode()
method without overriding it and still satisfy the conditions required by
the project specifications. I have tried, again and again, to recreate the
behaviour where multiple objects with identical instance variables created
the same hash using Object's default hashCode method. Would this individual
please supply me with the source code which produced this interesting
result? In the following piece of code, you will note that each object,
although having equal instance variables, returns different hash values.
public class Temp {
private int x;
public Temp( int i ) {
x = i;
}
public static void main( String[] args ) {
Temp[] a = new Temp[1000];
for ( int i = 0; i < a.length; i++ ) {
a[i] = new Temp( 0 );
System.out.println( a[i].hashCode() );
}
for ( int i = 0; i < 1000; i++ ) {
Temp x = new Temp( 0 );
System.out.println( x.hashCode() );
}
}
}
If you can give me something which is reproducable, I would be very
interested.
Thank you,
Douglas
Douglas Harder
2005-01-19 13:55:48 UTC
Permalink
In this case, both 0/7 and 0/12 should be simplified to 0/1, so

Rational r = new Rational( 0, 7 );
Rational s = new Rational( 0, 12 );

System.out.println( r.equals( s ) ); // should print true

and therefore, since r equals s, they must both return the same hash code.

Douglas
Post by Asimo
for hashCode, is it correct to return the same hashCode for 0/7 and 0/12
since both equate to 0, my code produces the same hashCode for it.
Post by Douglas Harder
Hello,
Someone after class claimed that he could use Object's default hashCode()
method without overriding it and still satisfy the conditions required by
the project specifications. I have tried, again and again, to recreate
the
Post by Douglas Harder
behaviour where multiple objects with identical instance variables created
the same hash using Object's default hashCode method. Would this
individual
Post by Douglas Harder
please supply me with the source code which produced this interesting
result? In the following piece of code, you will note that each object,
although having equal instance variables, returns different hash values.
public class Temp {
private int x;
public Temp( int i ) {
x = i;
}
public static void main( String[] args ) {
Temp[] a = new Temp[1000];
for ( int i = 0; i < a.length; i++ ) {
a[i] = new Temp( 0 );
System.out.println( a[i].hashCode() );
}
for ( int i = 0; i < 1000; i++ ) {
Temp x = new Temp( 0 );
System.out.println( x.hashCode() );
}
}
}
If you can give me something which is reproducable, I would be very
interested.
Thank you,
Douglas
Loading...