equals和hashCode 面试题

一:手写equals和hashCode

1:创建学生实体类并手写equals和hashCode方法

public class StudentA {

    /**名字*/
    private String name;
    /**年龄*/
    private int age ;
    /**性别*/
    private String  sex ;
    /**是否有车*/
    private boolean hasCar;

    public StudentA() {
    }

    public StudentA(String name) {
        this.name = name;
    }

    public boolean equals(Object o){
       if(this==o){
           return true;
       }
       if(o==null || this.getClass()!=o.getClass()){
           return false;
       }
       StudentA studentA = (StudentA) o;
       return this.age==studentA.age 
              && this.name.equals(studentA.name) 
              && (this.sex==null?"":this.sex).equals(studentA.sex)
              && this.hasCar==(studentA.hasCar);
   }

   public int hashCode(){
       final int prime = 31;
       int result = 1;
       result= prime * result +age;
       result=prime * result + ((name==null)?0:name.hashCode());
       result=prime * result + ((sex==null)?0:sex.hashCode());
       result=prime * result + (hasCar ? 1231 : 1237);
       return result;
   }  
   
	public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public boolean isHasCar() {
        return hasCar;
    }

    public void setHasCar(boolean hasCar) {
        this.hasCar = hasCar;
    }
}

二: 重写equals和hashCode的原因(参考三:分析Object的equals和hashCode)

1:测试hashCode方法和equals方法

理论一: hashCode不相同,两个对象就不相同
public class TestStudent {

    public static void main(String[] args) {
        StudentA studentA1 = new StudentA("护士");
        studentA1.setAge(18);
        StudentA studentA2 = new StudentA("护士");
        studentA1.setAge(19);

        System.err.println("studentA1对象的hashCode值:"+ studentA1.hashCode());
        System.err.println("studentA2对象的hashCode值:"+ studentA2.hashCode());
        System.err.println("比较对象:"+ studentA1.equals(studentA2));    
    }
}
理论一输出: 
studentA1对象的hashCode值:775648362
studentA2对象的hashCode值:775082333
比较对象:false
理论二: hashCode相同,两个对象不一定相同
public class TestStudent {

    public static void main(String[] args) {
        StudentA studentA1 = new StudentA("护士");
        StudentA studentA2 = new StudentA("护士");

        System.err.println("studentA1对象的hashCode值:"+ studentA1.hashCode());
        System.err.println("studentA2对象的hashCode值:"+ studentA2.hashCode());
        System.err.println("比较对象:"+ studentA1.equals(studentA2));
    }
}
理论二输出: 
studentA1对象的hashCode值:775082333
studentA2对象的hashCode值:775082333
比较对象:false
理论三: equals相同,hashCode一定相同,两个对象一定相同
public class TestStudent {

    public static void main(String[] args) {
        StudentA studentA1 = new StudentA("护士");
        studentA1.setAge(18);
        studentA1.setHasCar(false);
        studentA1.setSex("男");
        StudentA studentA2 = new StudentA("护士");
        studentA2.setAge(18);
        studentA2.setHasCar(false);
        studentA2.setSex("男");

        System.err.println("studentA1对象的hashCode值:"+ studentA1.hashCode());
        System.err.println("studentA2对象的hashCode值:"+ studentA2.hashCode());
        System.err.println("比较对象:"+ studentA1.equals(studentA2));
    }
}
理论三输出:
studentA1对象的hashCode值:776548788
studentA2对象的hashCode值:776548788
比较对象:true

2:特殊hashCode方法和equals方法(自定义需求,如果名字相同的两个对象认为是同一个人)

public class StudentA {

    /**名字*/
    private String name;
    /**年龄*/
    private int age ;
    /**性别*/
    private String  sex ;
    /**是否有车*/
    private boolean hasCar;

    public StudentA() {
    }

    public StudentA(String name) {
        this.name = name;
    }

   public boolean equals(Object o){
       if(this==o){
           return true;
       }
       if(o==null || this.getClass()!=o.getClass()){
           return false;
       }
       StudentA studentA = (StudentA) o;
       return this.name.equals(studentA.name);
   }

    public int hashCode(){
        final int prime = 31;
        int result = 1;
        result=prime * result + ((name==null)?0:name.hashCode());
        return result;
    }
    
}

3:特殊hashCode方法和equals方法测试

public class TestStudent {

    public static void main(String[] args) {
        StudentA studentA1 = new StudentA("护士");
        StudentA studentA2 = new StudentA("护士");

        System.err.println("studentA1对象的hashCode值:"+ studentA1.hashCode());
        System.err.println("studentA2对象的hashCode值:"+ studentA2.hashCode());
        System.err.println("比较对象:"+ studentA1.equals(studentA2));

    }
}
studentA1对象的hashCode值:805606
studentA2对象的hashCode值:805606
比较对象:true

三:分析Object的equals和hashCode

1:通过分析Object的equals方法,能够看到Object中equals比较的是地址值,如果在子类中不覆写equals,就会调用父类Object中的equals方法,因比较内存地址值而导致两个对象不等,但是在实际开发中存在两个不同的实体,在业务逻辑上是相同的,如果不覆写equals,不足以满足开发条件
2:覆写了equals方法,必须覆写hashCode,因为“相等的对象必须具有相等的散列码”
在这里插入图片描述

四:总结

  1. 注意点:比较两个对象先判断hashCode值,当hashCode值相同才会去比较equals,如果hashCode值不相同,直接返回false,不会去比较equals
  2. 注意点:equals是绝对的,hashCode是辅助条件
  3. 结论:①:如果两个对象的equals是相同的,那么他们的hashCode也是相同的
    ②:如果两个对象的equals不相同,那么他们的hashCode有可能是相同的
    ③:如果两个对象的hashCode是相同的,两个对象不一定是相同的
    ④:如果两个对象的hashCode不相同,两个对象一定不相同
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值