Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Making a hash of hashCode

Posted on 2006-06-27
5
Medium Priority
?
329 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
  • 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 2000 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

Independent Software Vendors: 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

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

926 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