java重写equals和hashCode方法

重写equals方法后记得把hashCode也重写一下,hashCode方法是返回java记录对象散列码,散列码尽量不要在对象值不同的情况下重复。看代码先

public class ETest
{
    private String name;

    private String[] info;

    private int id;

    private float money;

    private boolean freeze;

    private byte status;

    private long saving;

    @Override
    public boolean equals(Object obj)
    {

        if (this == obj)
        {
            return true;
        }
        if (obj instanceof ETest)
        {
            ETest et = (ETest) obj;
            if (et.id == this.id
                    && (et.name == this.name || et.name.equals(this.name))
                    && (Arrays.equals(et.info, this.info))
                    && et.money == this.money && et.freeze == this.freeze
                    && et.status == this.status && et.saving == this.saving
            )
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 
     * {@inheritDoc}
     */
    @Override
    public int hashCode()
    {
        // 正整数常量,值为素数
        int result = 17;
        // 计算hashCode,37是素数
        
        // 整数
        result = 37 * result + id;
        // 对象
        result = 37 * result + objectHashCode(name);
        // 浮点型
        result = 37 * result + Float.floatToIntBits(money);
        // boolean
        result = 37 * result + (freeze ? 0 : 1);
        // byte
        result = 37 * result + (int) status;
        // Long
        result = 37 * result + (int) (saving ^ (saving >>> 32));
        // 数组
        result = 37 * result + arrayHashCode(info);
        return result;
    }

    int objectHashCode(Object obj){
         
        if (obj == null)
        {
            return 0;
        }
        return obj.hashCode();
    }
    
    int arrayHashCode(Object[] objs)
    {
        int result = 0;
        if (objs == null)
        {
            return result;
        }
        for (Object o : objs)
        {
            result += objectHashCode(o);
        }
        return result;
    }
    
    public ETest(String name, String[] info, int id, float money,
            boolean freeze, byte status, long saving)
    {
        super();
        this.name = name;
        this.info = info;
        this.id = id;
        this.money = money;
        this.freeze = freeze;
        this.status = status;
        this.saving = saving;
    }
    
}

测试
public class Main
{
     public static void main(String[] args)
    {
        ETest t1 = new ETest(null, new String[]{null,"dd"}, Integer.MAX_VALUE, Integer.MIN_VALUE, false, (byte) 0, 0);
        ETest t2 = new ETest(null, new String[]{null,"dd"}, Integer.MAX_VALUE, Integer.MIN_VALUE, false, (byte) 0, 0);
        
        Set<ETest> set = new HashSet<ETest>();
        
        set.add(t1);
        set.add(t2);
        
        System.out.println(set.size());
        
        
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值