Solved

Making a hash of hashCode

Posted on 2006-06-27
5
317 Views
Last Modified: 2007-12-19
Dear Experts,

I have some simple data beans that need to override hashCode.  One of you Experts gave me a simple hasCode example a while back, which I implemented like this:

      public int hashCode()
      {
            int hash = 17;
            hash = hash * 37 + bankID;
            hash = hash * 37 + applicationCode;
            hash = hash * 37 + (int) accountNbr; // this is a long
            return hash;
      }

My problem is, at least on this example, accountNbr is a long, not an int.  (It a ten-byte numeric.)  So I'm obviously losing some precision here.  

My question is: Is that a little bit bad, or just plain wrong?  (If it just means less-than-optimal sorting, for instance, that's OK for my purposes.)  

And if it's just plain wrong, what do I do?  hashCode needs to return an int, right?

Thanks again...
BrianMc1958
0
Comment
Question by:BrianMc1958
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:BrianMc1958
ID: 16992413
BTW: bankID and applicationCode are both ints.
0
 
LVL 4

Expert Comment

by:fffej78
ID: 16992730
If you are using Java5, you could try using a prebuilt hashcode function instead.

import java.util.Arrays;

public int hashCode()
{
  return Arrays.hashCode( new long[] {bankID, applicationCode, accountNbr } );
}

I don't think your method is wrong.  Hashcode generally isn't used for sorting, it is for hash maps and the like though.
0
 

Author Comment

by:BrianMc1958
ID: 16992944
Actually, the whole hashCode thing is still something of a mystery to me.  When I mentioned sorting, it was with hash maps in mind.  My vague understanding is that the hashCode result (as opposed the the "equals" result) does not need to be unique for all objects.   It's one of the few times in our profession where close is good enough.  Is that correct?
0
 
LVL 4

Accepted Solution

by:
fffej78 earned 500 total points
ID: 16993080
In a hash table, collisions are bad and adversely affect look-up time.  In general, you are right, there is no such thing as a perfect hash code for any sufficiently large data set.  If you know all the possibilities you can generate a perfect hash function, see for example http://burtleburtle.net/bob/hash/perfect.html

What you don't want is a hash function that collides very often.  As long as you follow the standard guidelines (see http://www.javapractices.com/Topic28.cjp ), you should be fine.  Guidelines reproduced below:  

    * if a class overrides equals, it must override hashCode
    * if two objects are equal, then their hashCode values must be equal as well
    * if a field is not used in equals, then it must not be used in hashCode
    * if it is accessed often, hashCode is a candidate for caching

Java doesn't have a sorted hash map, though it does have a sorted map.  See http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeMap.html

I'm not sure why you'd want to sort the bean hashcode though.  Can you give some examples of how you intend to use this hashcode?  Or what you intend doing with the bean in the way of collections etc?
0
 

Author Comment

by:BrianMc1958
ID: 16993180
Actually, you've answered my question.  I shouldn't have mentioned sorting in the first place.  I just think of hashing as being like sorting, which is off the point here.  In my case, a collision will occur very rarely, so it looks like I'm OK.

Thanks a lot!
--BrianMc1958.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat administrtor 12 68
difference between sorce folder and folder in eclipise 3 40
Print Rhino Java Array in Javascript 1 31
Java array sort 10 29
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question