search

Home  >  Q&A  >  body text

初涉继承,关于java中重写hashcode()方法的问题

大家讲道理大家讲道理2801 days ago950

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:47:27

    Common convention for implementing hashCodemethods

    1. During the execution of the application, as long as the information used in the comparison operation of the object's equals method is not modified, then the same object is called multiple times, and the equals方法的比较操作所用到的信息没有被修改,那么对这个同一对象调用多次,hashCodemethod must consistently return the same integer. During multiple executions of the same application, the integers returned by each execution may be inconsistent.

    2. If two objects are equal according to the equals(Object) method, then calling the equals(Object)方法比较是相等的,那么调用这两个对象中任意一个对象的hashCode方法都必须产生同样的整数结果。反之,如果两个对象hashCode方法返回整数结果一样,则不代表两个对象相等,因为equalsmethod of either object must produce the same integer result

      . On the contrary, if the
    3. method of two objects returns the same integer result, it does not mean that the two objects are equal, because the equals method can be overloaded.
    4. equals(Object)方法比较是不相等的,那么调用这两个对象中任意一个对象的hashCode

      If two objects are not equal according to the equals(Object) method, then calling the
    5. method of either object does not necessarily produce different integer results. However, if you can make different objects produce different integer results, it is possible to improve the performance of the hash table.

    hashCode

    Hash code calculation (from: Effective Java)
    1. 17,保存在一个名为resultint

      Put a non-zero constant value, such as a variable of type
    2. .
    3. f(equals For each key field in the object

      and each field involved in the method
        ), complete the following steps:
      1. int

        Compute a hash code c of type
          for this domain:
        1. boolean类型,则计算(f?1:0

          if the domain is
        2. ).
        3. bytecharshort或者int类型,则计算(int)f

          If the domain is
        4. .
        5. long类型,则计算(int)(f^(f>>>32))

          If the domain is
        6. .
        7. float类型,则计算Float.floatToIntBits(f)

          If the domain is
        8. .
        9. double类型,则计算Double.doubleToLongBits(f),然后按照步骤2.1.3,为得到的long

          If the field is of double type, calculate Double.doubleToLongBits(f), and then follow step 2.1.3
        10. to get the long Type value computes a hash value.
        11. equals方法通过递归地调用equals的方式来比较这个域,则同样为这个域递归地调用hashCode。如果需要更复杂的比较,则为这个域计算一个范式(canonical representation),然后针对这个范式调用hashCode。如果这个域的值为null,则返回0

          If the field is an object reference, and the equals method of the class compares the field by recursively calling equals, it is also called recursively for this field. 🎜. If a more complex comparison is required, compute a canonical representation (canonical representation) for the domain and then call 🎜 against this paradigm. If the value of this field is null, 0 is returned (other constants are also acceptable). 🎜
        12. If the field is an array, each element should be treated as a separate field. That is, apply the above rules recursively, calculate a hash code for each significant element, and then combine these hash values ​​according to step 2.2. If every element in the array field is important, you can take advantage of one of the methods added in release 1.5Arrays.hashCode.

      2. According to the following formula, the hash code calculated in step 2.1 c合并到result中:result = 31 * result + c; //此处31 is an odd prime number, and has a good feature, that is, using shifting and subtraction instead of multiplication, you can get better performance: `31* i == (i<<5) - i, modern JVM can automatically complete this optimization.

    4. Returnresult

    5. Verify and test whether the hashCodeimplementation conforms to common conventions.


    Example implementation

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + (origin == null ? 0 : origin.hashCode());
        result = 31 * result + (hsNumber == null ? 0 : hsNumber.hashCode());
        result = 31 * result + (imageUrl == null ? 0 : imageUrl.hashCode());
        result = 31 * result + (classificationName == null ? 0 : classificationName.hashCode());
        return result;
    }

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:47:27

    Java’s int is fixed to 32 bits. In addition, your latitude and longtitude are double... I think it will be 64-bit.

    hashcode and equals have agreed semantics. You can take a look at Object

    I think the equals you wrote can be used.


    Note: The contract in the Object class is actually a very weak constraint. We can write hashcode() and equals() like this without violating the contract;

    public int hashcode() {
        return 0;
    }
    
    public boolean equals(Object o) {
        return (o != null) && (o.getClass() == getClass());
    }

    So the real question is how you define equality. Code is secondary.
    If equality is defined as "longitude and latitude are equal respectively", then the code you gave is a usable solution (but not the only available solution).

    reply
    0
  • Cancelreply