hashCode()与equals()连用

before

package com.example.demo.model;

public class Before {

    private String name = "稍后123";
    private Short age = 2;

    public Before(String name, Short age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object object){
        if(object instanceof Before){
            String tempName = ((Before)object).name;
            Short tempAge = ((Before)object).age;
            return this.name.equals(tempName)&&this.age.equals(tempAge);
        }
        return  false;
    }
}
package com.example.demo;

import com.example.demo.model.After;
import com.example.demo.model.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
        HashSet<Before> hashSet = new HashSet<>();
        //生成临时变量1
        Before before1 = new Before("稍等123",(short)2);
        //生成临时变量2
        Before before2 = new Before("稍等123",(short)2);
        //添加同一对象
        hashSet.add(before1);
        hashSet.add(before2);
        System.out.println("添加同一对象后,HashSet大小:"+hashSet.size());

    }

}

打印 添加同一对象后,hashSet大小:2

现象:任何类都是从object继承而来,如果没有重写equals()方法,默认比较的是对象类的地址。同样如果没有重写hashCode()方法,hash值也是根据对象地址来计算的。判断两个对象是否相等依据的是是否有相同的hashCode值和equals值。漏写任何一个都有可能出现意外结果

解决:同时重写equals()方法和hashCode()方法

after

package com.example.demo.model;

public class After {
    private String name = "稍等123";
    private Short age = 1;

    public After(String name, Short age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Short getAge() {
        return age;
    }

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

    //重写hashCode方法
    @Override
    public int hashCode() {
        //第一步设定平衡因子
        final int balanceFactor = 17;
        int result = balanceFactor;
        //第二步计算基本类型的hash值
        result = result*balanceFactor+age;
        //第三步计算非基本类型成员变量hash值
        return result*balanceFactor+name.hashCode();
    }

    //重写equals方法
    @Override
    public boolean equals(Object object){
        if(object instanceof After){
            String tempName = ((After)object).name;
            Short tempAge = ((After)object).age;
            return this.name.equals(tempName)&&this.age.equals(tempAge);
        }
        return  false;
    }
}
package com.example.demo;

import com.example.demo.model.After;
import com.example.demo.model.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
        HashSet<After> hashSet = new HashSet<>();
        //生成临时变量1
        After after1 = new After("稍等123",(short)1);
        //生成临时变量2
        After after2 = new After("稍等123",(short)1);
        //添加同一对象
        hashSet.add(after1);
        hashSet.add(after2);
        System.out.println("添加同一对象后,HashSet大小:"+hashSet.size());

        //直接删除
        hashSet.remove(after2);
        System.out.println("直接删除对象后,HashSet大小:"+hashSet.size());
        //修改参与计算的hashCode的属性后再删除
        hashSet.add(after1);
        after1.setName("3244DJSJ");
        hashSet.remove(after1);
        System.out.println("修改参与计算hashCode的属性后,再删除对象,HashSet大小:"+hashSet.size());
        //清空hashSet
        hashSet.clear();
        System.out.println("清空hashSet,HashSet大小:"+hashSet.size());
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值