Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

hashCode for 4 ints?

Thread view: 
Jeff - 21 Jun 2005 00:39 GMT
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;
   }
}

Signature

Jeff

YoungHwan - 21 Jun 2005 01:08 GMT
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]
>     }
> }
Lasse Reichstein Nielsen - 21 Jun 2005 01:12 GMT
> 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.'

Eric Sosman - 21 Jun 2005 15:30 GMT
> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.