Solved

Making a hash of hashCode

Posted on 2006-06-27
5
321 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

710 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