覆盖equals时总要覆盖hasCode

覆盖equals时总要覆盖hasCode

在每个覆盖了equals方法的类中,也必须覆盖hashCode方法。如果不这样做的话,违反Object hashCode的通用约定,从而导致该类无法结合所有基于散列的集合一起正常运作,这样的集合包括HashMap,HashSet,Hashtable。


例如:

import java.util.*;

public final class PhoneNumber {
    private final short areaCode;
    private final short prefix;
    private final short lineNumber;

    public PhoneNumber(int areaCode, int prefix,
                       int lineNumber) {
        rangeCheck(areaCode,    999, "area code");
        rangeCheck(prefix,      999, "prefix");
        rangeCheck(lineNumber, 9999, "line number");
        this.areaCode  = (short) areaCode;
        this.prefix  = (short) prefix;
        this.lineNumber = (short) lineNumber;
    }

    private static void rangeCheck(int arg, int max,
                                   String name) {
        if (arg < 0 || arg > max)
           throw new IllegalArgumentException(name +": " + arg);
    }

    @Override public boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof PhoneNumber))
            return false;
        PhoneNumber pn = (PhoneNumber)o;
        return pn.lineNumber == lineNumber
            && pn.prefix  == prefix
            && pn.areaCode  == areaCode;
    }

    // Broken - no hashCode method!

    // A decent hashCode method - Page 48
//  @Override public int hashCode() {
//      int result = 17;
//      result = 31 * result + areaCode;
//      result = 31 * result + prefix;
//      result = 31 * result + lineNumber;
//      return result;
//  }


    // Lazily initialized, cached hashCode - Page 49
//  private volatile int hashCode;  // (See Item 71)
//
//  @Override public int hashCode() {
//      int result = hashCode;
//      if (result == 0) {
//          result = 17;
//          result = 31 * result + areaCode;
//          result = 31 * result + prefix;
//          result = 31 * result + lineNumber;
//          hashCode = result;
//      }
//      return result;
//  }

    public static void main(String[] args) {
        Map<PhoneNumber, String> m
            = new HashMap<PhoneNumber, String>();
        m.put(new PhoneNumber(707, 867, 5309), "Jenny");
        System.out.println(m.get(new PhoneNumber(707, 867, 5309)));
    }
}

console控制台如下:

Connected to the target VM, address: '127.0.0.1:51509', transport: 'socket'
null
Disconnected from the target VM, address: '127.0.0.1:51509', transport: 'socket'

Process finished with exit code 0


很奇怪,为什么  

m.get(new PhoneNumber(707, 867, 5309))  拿不到  "Jenny"

hasMap  put   进去的是什么    get的时候是什么

或者说  get的时候,根据get的值对象,没有找到,返回一个null


Object.java

 /**
     * Returns a hash code value for the object. This method is 
     * supported for the benefit of hashtables such as those provided by 
     * <code>java.util.Hashtable</code>. 
     * <p>
     * The general contract of <code>hashCode</code> is: 
     * <ul>
     * <li>Whenever it is invoked on the same object more than once during 
     *     an execution of a Java application, the <tt>hashCode</tt> method 
     *     must consistently return the same integer, provided no information 
     *     used in <tt>equals</tt> comparisons on the object is modified.
     *     This integer need not remain consistent from one execution of an
     *     application to another execution of the same application. 
     * <li>If two objects are equal according to the <tt>equals(Object)</tt>
     *     method, then calling the <code>hashCode</code> method on each of 
     *     the two objects must produce the same integer result. 
     * <li>It is <em>not</em> required that if two objects are unequal 
     *     according to the {@link java.lang.Object#equals(java.lang.Object)} 
     *     method, then calling the <tt>hashCode</tt> method on each of the 
     *     two objects must produce distinct integer results.  However, the 
     *     programmer should be aware that producing distinct integer results 
     *     for unequal objects may improve the performance of hashtables.
     * </ul>
     * <p>
     * As much as is reasonably practical, the hashCode method defined by 
     * class <tt>Object</tt> does return distinct integers for distinct 
     * objects. (This is typically implemented by converting the internal 
     * address of the object into an integer, but this implementation 
     * technique is not required by the 
     * Java<font size="-2"><sup>TM</sup></font> programming language.)
     *
     * @return  a hash code value for this object.
     * @see     java.lang.Object#equals(java.lang.Object)
     * @see     java.util.Hashtable
     */
    public native int hashCode();



打开覆盖的hasCode,

console控制台:

Connected to the target VM, address: '127.0.0.1:51736', transport: 'socket'
Jenny
Disconnected from the target VM, address: '127.0.0.1:51736', transport: 'socket'

Process finished with exit code 0


很土的理解:散性函数返回结果不一样。导致get拿到的是空。

重写覆盖后,我们能正确的拿到自己想要的Value



相等的实例是不是具有相等的散列码?






<think>我们正在讨论Java中重写equals方法为何必须同重写hashCode方法。根据引用内容,主要原因包括: 1. 一致性原则:Java规定,如果两个对象通过equals比较相等,那么它们的hashCode必须相等。如果只重写equals而不重写hashCode,可能违反此原则。 2. 哈希集合的正确性:基于哈希的集合(如HashMap、HashSet)依赖hashCode来定位存储位置。如果两个相等对象hashCode不同,集合会将其视为不同对象,导致错误行为。 3. 性能考虑:正确实现的hashCode可以提高哈希集合的查找效率。 下面详细解释: ### 1. Java对象约定 在Java中,`equals`和`hashCode`方法在`Object`类中定义,并有一组约定(contract): - 如果两个对象根据`equals`方法比较是相等的,那么调用这两个对象的`hashCode`方法必须产生相同的整数结果。 - 如果两个对象根据`equals`方法比较是不相等的,那么它们的`hashCode`可以相同也可以不同(但不同有助于提高哈希表性能)。 因此,当我们重写`equals`方法以改变对象相等的语义,必须同重写`hashCode`方法以保持这一约定。否则,即使两个对象在逻辑上相等(`equals`返回`true`),它们的`hashCode`可能不同(因为未重写使用的是`Object`的默认实现,即基于内存地址计算),从而违反约定。 ### 2. 哈希集合的影响 哈希集合(如`HashSet`, `HashMap`)使用哈希码(`hashCode`)来组织数据。当向集合中添加元素: - 首先计算对象的`hashCode`,根据该值确定存储位置(桶)。 - 如果两个对象相等(`equals`返回`true`),但`hashCode`不同,它们会被放入不同的桶中,导致集合中出现重复元素(逻辑上相等但存储多次)。 - 同样,在查找,即使集合中存在一个相等的对象,也可能因为不同的`hashCode`而找不到。 例如,考虑以下未重写`hashCode`的类: ```java class Person { private String name; private int age; // 构造方法等 @Override public boolean equals(Object obj) { // 比较name和age // ... } } ``` 然后使用`HashSet`: ```java Person p1 = new Person("Alice", 30); Person p2 = new Person("Alice", 30); Set<Person> set = new HashSet<>(); set.add(p1); set.add(p2); ``` 由于`p1`和`p2`在逻辑上相等(根据`equals`),我们期望集合中只有一个元素。但如果没有重写`hashCode`,`p1`和`p2`的默认`hashCode`(基于地址)不同,因此`HashSet`会认为它们是两个不同的对象,从而存储两个元素。这违反了集合的唯一性要求。 ### 3. 如何重写hashCode 重写`hashCode`,通常使用对象的相同属性来计算哈希值,这些属性也用于`equals`比较。例如,在`Person`类中: ```java @Override public int hashCode() { return Objects.hash(name, age); // 使用name和age计算哈希值 } ``` 这样,只要两个`Person`对象的`name`和`age`相同,它们的`hashCode`就会相同,从而满足约定。 ### 总结 在Java中重写`equals`方法,必须同重写`hashCode`方法,以确保: - 遵守Java的对象约定。 - 基于哈希的集合(如`HashMap`, `HashSet`)能正确工作。 - 避免难以察觉的错误。 违反这一原则可能导致程序行为异常,尤其是在使用哈希集合。 §§相关问题§§ 1. 如果不重写hashCode方法,会对HashMap的性能产生什么影响? 2. 重写hashCode方法有哪些最佳实践? 3. 除了哈希集合,还有哪些场景会受到equals和hashCode不一致的影响? 4. 如何确保重写的equals和hashCode方法满足对称性、传递性和一致性?</think>### Java中重写equals方法为何必须重写hashCode方法 在Java中,重写`equals`方法必须同重写`hashCode`方法,主要原因如下: #### 1. **遵守Java对象约定** Java规范要求:**如果两个对象通过`equals`比较相等,则它们的`hashCode`必须相同**。 - 若不重写`hashCode`,即使`equals`判定两个对象逻辑相等(属性相同),其默认的`hashCode`(基于内存地址)仍会不同,违反约定[^1][^4]。 - 示例: ```java Person p1 = new Person("Alice", 30); Person p2 = new Person("Alice", 30); p1.equals(p2); // true(重写equals后) p1.hashCode() != p2.hashCode(); // true(未重写hashCode) ``` #### 2. **确保哈希集合的正确性** 基于哈希的集合(如`HashMap`、`HashSet`)依赖`hashCode`定位存储位置: - 若相等的对象`hashCode`不同,集合会将其视为不同对象,导致: - **重复存储**:逻辑相等的对象被多次存入集合 - **查找失败**:`map.get(p2)`可能返回`null`,即使`p2`逻辑等于`p1`[^2][^3] - 示例: ```java Set<Person> set = new HashSet<>(); set.add(p1); set.contains(p2); // false(未重写hashCode) ``` #### 3. **性能优化** 合理的`hashCode`实现能减少哈希冲突,提升哈希表操作效率: - 默认`hashCode`(内存地址)无法反映对象内容,导致哈希分布不均 - 重写后基于关键属性计算哈希,可优化桶分配[^4] #### ✅ 正确实现示例 ```java public class Person { private String name; private int age; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } @Override public int hashCode() { // 使用相同属性计算哈希值 return Objects.hash(name, age); } } ``` ### 关键结论 | 场景 | 未重写`hashCode`的后果 | 重写后的效果 | |--------------------|--------------------------------|--------------------------| | 对象比较 | 违反`equals-hashCode`约定 | 满足Java规范 | | 哈希集合操作 | 存储重复项、查找失败 | 集合行为正确 | | 系统性能 | 哈希冲突率高,效率低下 | 优化哈希分布 | > **总结**:重写`hashCode`是维护对象一致性、确保哈希集合正确性和优化性能的必要措施[^3][^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值