what about this??
public int hashCode() {
int result = 17;
result = 37 * result + highest;
result = 37 * result + high;
result = 37 * result + low;
result = 37 * result + lowest;
return result;
}
>From ' Effective Java: Programming Language Guide
Jeff =9E=84:
> Am I misusing HashMap or just need to fix the .hashCode() override for the
> key object?
[quoted text clipped - 50 lines]
> }
> }
> The class I use as a key for a HashMap is unique based on a combination of 4
> ints.
Ok, that gives you (2^32)^4 different possible values (or 2^96 times
more than the possible values of hashCode()).
> Normally, about 5000 instances are created and persist over time.
> The maximum possible created is probably max around 40,000.
Is there any system to these, or are they distributed randomly in
int^4-space?
> Since .hashCode() returns an int, how do I create a unique identifier for
> the 128 bits contained in the 4 ints?
Obviously, you can't map 2^128 bits of information uniquely into 2^32
bits, unless you know something about the four integers that you
haven't told us yet.
> Or should I swap to another data structure?
No need. Hash values doesn't need to be unique. For performance
reasons, it's best that they don't collide too often, but for 40000
instances, that's unlikely to happen.
> public boolean equals( Object arg0 ) {
>
[quoted text clipped - 5 lines]
> else
> return false;
Just do:
return (A==other.A) && ... && (D == other.D);
> // how do I create a hashcode to lookup the unique combination of the 4
> ints? The following obviously wouldn't work,
No simple method will guarantee uniqueness.
Maybe it is possible to make a simple method that works for a typical
choice of the 40000 instances, but as long as it's fairly good, a few
collisions won't matter much.
> // but hopefully indicates my need
> public int hashCode() {
How about just:
return ((((((A * 31) + B) * 31) + C) * 31) + D);
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
> Am I misusing HashMap or just need to fix the .hashCode() override for the
> key object?
Others have given advice about how to hash, but I haven't
seen anyone point out a different problem in your code:
> public boolean equals( Object arg0 ) {
>
> IntHashExample other = ( IntHashExample ) arg0;
This violates the equals() contract. If `arg0' is
a String or a JPanel or anything except an IntHashExample,
this method will throw a ClassCastException -- but equals()
is not supposed to do that; it should just return false.
Also, if `arg0' is null `other' will also be null, and
you'll throw a NullPointerException as soon as you try to
access the non-existent object it doesn't reference.
Suggested fix:
public boolean equals(Object arg0) {
if (! (arg0 instanceof IntHashExample) )
return false;
// the remainder as before ...

Signature
Eric.Sosman@sun.com
Wibble - 22 Jun 2005 02:28 GMT
>>Am I misusing HashMap or just need to fix the .hashCode() override for the
>>key object?
[quoted text clipped - 20 lines]
> return false;
> // the remainder as before ...
Nopers, instanceof isnt strong enough since arg0 may subclass
IntHashExample and override hashCode or equals. use
if (arg0==null || arg0.getClass()==this.getClass()) return(false);
John C. Bollinger - 22 Jun 2005 04:00 GMT
[...]
>>> public boolean equals( Object arg0 ) {
>>>
>>> IntHashExample other = ( IntHashExample ) arg0;
>> This violates the equals() contract.
[...]
>> Suggested fix:
>>
[quoted text clipped - 7 lines]
>
> if (arg0==null || arg0.getClass()==this.getClass()) return(false);
It depends on your point of view, I suppose. instanceof works fine for
the equals() method described, but it does provide an extra avenue by
which a subclass can be broken (overriding equals() or hashCode()). The
class presented is only broken if such a subclass exists. Such breakage
could also be prevented by making the class final (which renders your
solution equivalent to Eric's) or by making just equals() and hashCode()
final.

Signature
John Bollinger
jobollin@indiana.edu