equals, hashCode, compareTo对比分析

2020-05-22  本文已影响0人  啊啊啊哼哼哼

首先,我们回顾一下Java Collection体系的核心约定:

因此,基于equals和hashCode的特性,Java要求我们在重写equals方法的同时也要重写hashCode方法,使得它们永远保持上面的关系。

除了hashCode要与equals保持一致,compareTo方法也要和equals爆出一致,TreeSet在比较对象时调用compareTo来比较,如果compareTo和equals表达的相等的含义不同,程序会出现违背编程者行为的错误,比如:

package test;

import java.util.Objects;
import java.util.TreeSet;

public class Shop implements Comparable<Shop> {
    // 主键ID
    private Long shopId;
    // 店铺名称
    private String shopName;

    public Shop(Long shopId, String shopName) {
        this.shopId = shopId;
        this.shopName = shopName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Shop shop = (Shop) o;
        return Objects.equals(shopId, shop.shopId) &&
                Objects.equals(shopName, shop.shopName);

    }

    @Override
    public int hashCode() {
        return Objects.hash(shopId, shopName);
    }

    public Long getShopId() {
        return shopId;
    }

    public void setShopId(Long shopId) {
        this.shopId = shopId;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    @Override
    public String toString() {
        return "Shop{" +
                "shopId=" + shopId +
                ", shopName='" + shopName + '\'' +
                '}';
    }

    @Override
    public int compareTo(Shop that) {
        return shopId.compareTo(that.shopId);
    }

    public static void main(String[] args) {
        TreeSet<Shop> set = new TreeSet<>();
        set.add(new Shop(1L, "qinxiaoyu"));
        set.add(new Shop(1L, "wangzhaoning"));
        System.out.println(set);
    }
}
//output:
//[Shop{shopId=1, shopName='qinxiaoyu'}]

上述代码我们定义了一个Shop类,有一个Long类型的shopId属性和一个String类型的shopName属性,并且只有这两个属性分别相等,equals返回true。但是我们重写,compareTo方法时,我们错误的只写了shopId属性的比较,导致在我们添加TreeSet时,只有"qinxiaoyu"被添加,"wangzhaoning"通过compareTo方法判定和"qinxiaoyu"相同,未被添加。总结来说,因为equals方法和compareTo方法的不一致性导致了程序出现错误。

另一个需要注意的问题是,在我们重写compareTo方法时,或者Comparator里的compare方法时,我们正确的重写compareTo的方法:

public int compareTo(User that){
    if(this.id<that.id) return -1;
    else if(this.id == that.id) return 0;
    else return 1;
}

下面这种错误的写法,会造成严重的溢出问题:

public int compareTo(User that){
    return this.id - that.id;
}

经过了上面的分析,我们将Shop类修改正确:

package test;

import java.util.Comparator;
import java.util.Objects;
import java.util.TreeSet;

public class Shop {
    // 主键ID
    private Long shopId;
    // 店铺名称
    private String shopName;

    public Shop(Long shopId, String shopName) {
        this.shopId = shopId;
        this.shopName = shopName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Shop shop = (Shop) o;
        return Objects.equals(shopId, shop.shopId) &&
                Objects.equals(shopName, shop.shopName);

    }

    @Override
    public int hashCode() {
        return Objects.hash(shopId, shopName);
    }

    public Long getShopId() {
        return shopId;
    }

    public void setShopId(Long shopId) {
        this.shopId = shopId;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    @Override
    public String toString() {
        return "Shop{" +
                "shopId=" + shopId +
                ", shopName='" + shopName + '\'' +
                '}';
    }

    public static void main(String[] args) {
        TreeSet<Shop> set = new TreeSet<Shop>(Comparator.comparing(Shop::getShopId).
                thenComparing(Shop::getShopName));
        set.add(new Shop(1L, "qinxiaoyu"));
        set.add(new Shop(1L, "wangzhaoning"));
        System.out.println(set);
    }
}
//[Shop{shopId=1, shopName='qinxiaoyu'}, 
//Shop{shopId=1, shopName='wangzhaoning'}]
上一篇下一篇

猜你喜欢

热点阅读