对象相等比较

String的相等比较

对于String类型而言,一般用“==”或者equales做相等比较,前者比较字符串的引用,后者比较字符串的值。

字符串常量的值存储于常量池中,只要值相同,那么引用的就是同一个字符串常量,也就是说,==和equals效果一样。

字符串对象存储于堆中,不同的对象在堆上的内存地址是不一样的。因此,创建两个值相等的String对象,其引用的地址是不相等的。也就是说,用 == 比较时不等,用equals比较时相等。

Integer的相等比较

Integer是int的包装类型,通常直接赋值即可。比较时用==还是equals呢?看下面的例子:

Integer t1 = 129;
Integer t2 = 129;
Integer t3 = 119;
Integer t4 = 119;
System.out.println(t1 == t2);
System.out.println(t3 == t4);

输出结果是false,true。

原因是:对于在-128~127之间的数,Integer使用的是内部缓存值,在此范围之外的数,Integer会在堆上创建对象。t1和t2是堆上的不同对象,所以引用不相等。而t3和t4是内部缓存的值,引用相等。

显式new出来的Integer对象,必然会在堆上创建对象,其引用是不同的。
Integer和int进行==比较时,会自动拆箱进行值的比较。

HashMap中key的相等比较

对于HashMap的key,如果是基本数据类型,则直接进行值的比较。而如果是引用类型呢?

HashMap底层是用数组和链表存储的,当链表长度超过8时会转化为红黑树。根据key进行查找时,先根据hashCode找到数组上的位置,如果该位置上有多个元素,则继续通过equals方法判断值是否相等。

当两个复杂对象作为map的key相等时,必然先要保证hashCode相同,其次equals也要为true。也就是说,使用复杂对象作为map的key时,需要重写hashCode和queals方法。

如果只重写hashCode方法不重写equals方法,则默认的equals方法会进行内存地址的比较,必然是不同的。如果不重写hashCode方法而仅仅重写equals方法,则首先数组上的位置就不同(当然,发生哈希碰撞时例外),对象自然不会相同。

看下面的例子:

public static class Student {

    private String name;

    private String id;

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

    public Student(String name, String id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

public static void main(String[] args) throws Exception {
    Student stu1 = new Student("aa", "1");
    Student stu2 = new Student("aa", "1");
    Map<Student, String> map = new HashMap<>();
    map.put(stu1,"1");
    map.put(stu2,"2");
    System.out.println(map.size());
}

输出为2。

在这个例子中,map中放入Student类型对象作为key,如果不重写比较方法的话,放入的对象是不同的(默认的比较方法继承于Object类,比较的是内存地址)。

重写hashCode和queals

对Student类的比较方法进行重写:

public static class Student {

    private String name;

    private String id;

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

    public Student(String name, String id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof Student)) {
            return false;
        }
        Student s = (Student) o;
        return Objects.equals(id, s.getId()) && Objects.equals(name, s.getName());
    }

}

public static void main(String[] args) throws Exception {
    Student stu1 = new Student("aa", "1");
    Student stu2 = new Student("aa", "1");
    Map<Student, String> map = new HashMap<>();
    map.put(stu1,"1");
    map.put(stu2,"2");
    System.out.println(map.size());
}

输出为1,说明stu2和stu1相同,添加元素的key相同时,后者覆盖了前者。

还可以用Apache Commons Lang中的API来实现重写:

@Override
public int hashCode() {
    return new HashCodeBuilder(17, 37).append(name).append(id).toHashCode();
}

@Override
public boolean equals(Object o) {
    if (o == this) return true;
    if (!(o instanceof Student)) {
        return false;
    }
    Student s = (Student) o;
    return new EqualsBuilder().append(name, s.getName()).append(id, s.getId()).isEquals();
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值