Best implementation for hashCode method for a collection

后端 未结 20 3089
难免孤独
难免孤独 2020-11-22 01:39

How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ?

相关标签:
20条回答
  • 2020-11-22 02:35

    If I understand your question correctly, you have a custom collection class (i.e. a new class that extends from the Collection interface) and you want to implement the hashCode() method.

    If your collection class extends AbstractList, then you don't have to worry about it, there is already an implementation of equals() and hashCode() that works by iterating through all the objects and adding their hashCodes() together.

       public int hashCode() {
          int hashCode = 1;
          Iterator i = iterator();
          while (i.hasNext()) {
            Object obj = i.next();
            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
          }
      return hashCode;
       }
    

    Now if what you want is the best way to calculate the hash code for a specific class, I normally use the ^ (bitwise exclusive or) operator to process all fields that I use in the equals method:

    public int hashCode(){
       return intMember ^ (stringField != null ? stringField.hashCode() : 0);
    }
    
    0 讨论(0)
  • 2020-11-22 02:37

    It is better to use the functionality provided by Eclipse which does a pretty good job and you can put your efforts and energy in developing the business logic.

    0 讨论(0)
  • 2020-11-22 02:37

    @about8 : there is a pretty serious bug there.

    Zam obj1 = new Zam("foo", "bar", "baz");
    Zam obj2 = new Zam("fo", "obar", "baz");
    

    same hashcode

    you probably want something like

    public int hashCode() {
        return (getFoo().hashCode() + getBar().hashCode()).toString().hashCode();
    

    (can you get hashCode directly from int in Java these days? I think it does some autocasting.. if that's the case, skip the toString, it's ugly.)

    0 讨论(0)
  • 2020-11-22 02:39

    I use a tiny wrapper around Arrays.deepHashCode(...) because it handles arrays supplied as parameters correctly

    public static int hash(final Object... objects) {
        return Arrays.deepHashCode(objects);
    }
    
    0 讨论(0)
  • 2020-11-22 02:42

    Just a quick note for completing other more detailed answer (in term of code):

    If I consider the question how-do-i-create-a-hash-table-in-java and especially the jGuru FAQ entry, I believe some other criteria upon which a hash code could be judged are:

    • synchronization (does the algo support concurrent access or not) ?
    • fail safe iteration (does the algo detect a collection which changes during iteration)
    • null value (does the hash code support null value in the collection)
    0 讨论(0)
  • 2020-11-22 02:42

    any hashing method that evenly distributes the hash value over the possible range is a good implementation. See effective java ( http://books.google.com.au/books?id=ZZOiqZQIbRMC&dq=effective+java&pg=PP1&ots=UZMZ2siN25&sig=kR0n73DHJOn-D77qGj0wOxAxiZw&hl=en&sa=X&oi=book_result&resnum=1&ct=result ) , there is a good tip in there for hashcode implementation (item 9 i think...).

    0 讨论(0)
提交回复
热议问题