J
Jeff
Am I misusing HashMap or just need to fix the .hashCode() override for the
key object?
The class I use as a key for a HashMap is unique based on a combination of 4
ints. Normally, about 5000 instances are created and persist over time.
The maximum possible created is probably max around 40,000.
Since .hashCode() returns an int, how do I create a unique identifier for
the 128 bits contained in the 4 ints? Or should I swap to another data
structure?
Thanks!
// Here's a simplification of my class that illustrates my problem.
public class IntHashExample {
int A, B, C, D;
IntHashExample( int A , int B , int C , int D ) {
this.A = A;
this.B = B;
this.C = C;
this.D = D;
}
public boolean equals( Object arg0 ) {
IntHashExample other = ( IntHashExample ) arg0;
if ( ( A == other.A ) && ( B == other.B ) && ( C == other.C ) && ( D
== other.D ) )
return true;
else
return false;
}
// how do I create a hashcode to lookup the unique combination of the 4
ints? The following obviously wouldn't work,
// but hopefully indicates my need
public int hashCode() {
long highest = ( long ) A << 96;
long high = ( long ) B << 64;
long low = ( long ) C << 32;
long lowest = ( long ) D;
int code = ( int ) ( highest | high | low | lowest );
return code;
}
}
key object?
The class I use as a key for a HashMap is unique based on a combination of 4
ints. Normally, about 5000 instances are created and persist over time.
The maximum possible created is probably max around 40,000.
Since .hashCode() returns an int, how do I create a unique identifier for
the 128 bits contained in the 4 ints? Or should I swap to another data
structure?
Thanks!
// Here's a simplification of my class that illustrates my problem.
public class IntHashExample {
int A, B, C, D;
IntHashExample( int A , int B , int C , int D ) {
this.A = A;
this.B = B;
this.C = C;
this.D = D;
}
public boolean equals( Object arg0 ) {
IntHashExample other = ( IntHashExample ) arg0;
if ( ( A == other.A ) && ( B == other.B ) && ( C == other.C ) && ( D
== other.D ) )
return true;
else
return false;
}
// how do I create a hashcode to lookup the unique combination of the 4
ints? The following obviously wouldn't work,
// but hopefully indicates my need
public int hashCode() {
long highest = ( long ) A << 96;
long high = ( long ) B << 64;
long low = ( long ) C << 32;
long lowest = ( long ) D;
int code = ( int ) ( highest | high | low | lowest );
return code;
}
}