This discussion is archived
12 Replies Latest reply: Jun 24, 2010 8:03 AM by 3004 RSS

Overriding hashcode

843789 Newbie
Currently Being Moderated
When do we generally override hashcode method .Is there any standard way of overriding it ?

Is this a correct way to override hashcode()

public int hashCode()
{

return 1492;
}
  • 1. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    See java.lang.Object.hashcode() documentation:

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#hashCode%28%29

    returning a constant value is not a way to generate a valid hashcode, see the link for explanation.

    Too slow for sure. :-)
  • 2. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    anotherAikman wrote:
    returning a constant value is not a way to generate a valid hashcode
    It is valid just not a good way to do it ;)
  • 3. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    flounder wrote:
    anotherAikman wrote:
    returning a constant value is not a way to generate a valid hashcode
    It is valid just not a good way to do it ;)
    Many years ago I worked on a project for a bank. Some hash table C++ lookup code seemed to run very slowly so we profiled it. The offending hashcode() equivalent method was very like the OP's only used the value 0 tagged with a comment to the effect - "// This seems to work OK". We decided we should audit the offenders code more actively.
  • 4. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    Just for the sake of discussion(my learning, really):

    I think it should be invalid. How could that ever work?
    public class HoldingX
    {
        private int x;
        public HoldingX(int nx){x = nx;}
        public int hashcode(){return 42;}
    }
    Now if I put several instances of that class into some hash container, how would I retrieve exactly the one I want later on?
    I mean, it comes down to the Hashtable comparing the ints, doesnt it? Im half way off to work, so I cant look it up in src.zip.
  • 5. Re: Overriding hashcode
    masijade Explorer
    Currently Being Moderated
    anotherAikman wrote:
    Just for the sake of discussion(my learning, really):

    I think it should be invalid. How could that ever work?
    public class HoldingX
    {
    private int x;
    public HoldingX(int nx){x = nx;}
    public int hashcode(){return 42;}
    }
    Now if I put several instances of that class into some hash container, how would I retrieve exactly the one I want later on?
    I mean, it comes down to the Hashtable comparing the ints, doesnt it? Im half way off to work, so I cant look it up in src.zip.
    With a properly overridden equals method, of course.
  • 6. Re: Overriding hashcode
    Kayaman Guru
    Currently Being Moderated
    anotherAikman wrote:
    Now if I put several instances of that class into some hash container, how would I retrieve exactly the one I want later on?
    I mean, it comes down to the Hashtable comparing the ints, doesnt it? Im half way off to work, so I cant look it up in src.zip.
    If the hashcode is the same for 2 objects, they get stored in a list. They're still different from an equals() point of view. It just means that if your hashcode is always the same for all objects, you don't have a (for example) HashSet, you have a LinkedList because the hashing isn't effective.
  • 7. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    anotherAikman wrote:
    Just for the sake of discussion(my learning, really):

    I think it should be invalid. How could that ever work?
    public class HoldingX
    {
    private int x;
    public HoldingX(int nx){x = nx;}
    public int hashcode(){return 42;}
    }
    Now if I put several instances of that class into some hash container, how would I retrieve exactly the one I want later on?
    I mean, it comes down to the Hashtable comparing the ints, doesnt it? Im half way off to work, so I cant look it up in src.zip.
    You should define an equals() method as well a hashcode() method. In Java a Hashtable is just an array of linked lists (buckets). The hashcode() is used to determine which 'bucket' to look in then a linear search of the 'bucket' using the equals() method finds the desired object. Obviously one can make all the hash codes 42 and then all the objects go into one 'bucket' so it degenerates into one long linked list. For efficiency the hash codes should spread the objects roughly evenly between the 'buckets'.
  • 8. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    So to summarize: a hashcode method that returned a constant would still produce correct code (in that items added to a hashtable could be retrieved), but it throws away the efficiency advantage of a hash table, reducing it to a simple list.

    The real danger comes if you subtly diverge in your hashcode and equals implementations, such that two objects that evaluate as equal have different hashcodes. That actually breaks code.
  • 9. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    Thanks everybody for this enlightment not only about Hashtable and the like, but also on why to override equals() together with hashcode().

    Sorry to the OP for hijacking your thread, I hope the insights shared here were helpful for you , too.
  • 10. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    anotherAikman wrote:
    Thanks everybody for this enlightment not only about Hashtable and the like, but also on why to override equals() together with hashcode().
    Chapter 3 of Effective Java [which is available online for free|http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf] has great information about the topic.
  • 11. Re: Overriding hashcode
    843789 Newbie
    Currently Being Moderated
    Hey, thanks for that too. Too bad the OP seems headed to Alpha Centauri already. ;)
  • 12. Re: Overriding hashcode
    3004 Newbie
    Currently Being Moderated
    anotherAikman wrote:
    Thanks everybody for this enlightment not only about Hashtable and the like, but also on why to override equals() together with hashcode().
    Because hashCode() is not meant to be an identifier. It's kind of like a checksum on steroids. Think of it as the big lettered tab sticking out of your phone book that lets you get to the "D" section quickly. You still have to go through the Ds comparing names for equality until you find the one that equals "Duck, Donald."