Solved

Making a hash of hashCode

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

636 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