Java学习心得_Set集合元素的唯一性保证

前言

学到集合这一章,老师讲到Set集合中的元素是不重复的,手段则是通过重写hashCode()和equals()方法。本贴通过源码分析下是如何实现的。

先看下Set集合存入数据的大致流程:

先通过hashCode()方法得到元素的哈希值,通过哈希值计算对象的存储位置

  • 如果该存储位置没有元素,则存入数据
  • 如果有元素,遍历该位置所有元素的哈希值和存入数据的哈希值比较(Set集合底层为数组+链表)
    • 如果哈希值不相同,则存入元素
    • 如果哈希值相同,则调用equals方法比较内容是否相容,不同则存入元素,相同则表示重复。

这个流程中用到了hashCode和equals方法。

在Object中的hashCode方法是通过native关键字调用底层其他语言的方法进行实现的。

public native int hashCode();

这个方法是通过地址计算的对象的哈希值,也就是不同地址的对象哈希值不同。但是对于使用new关键字创建的对象,他们的地址肯定是不同的,但是其内容可能相同的。对于系统来说,我们关心的是对象的内容而不是地址,所以需要重写hashCode方法。

//重写hashCode方法前
Student s1  =new Student("张三");
Student s2  =new Student("张三");
System.out.println(s1);//Day06.Student@3f3afe78
System.out.println(s2);//Day06.Student@7f63425a
System.out.println(s1.hashCode());//1060830840
System.out.println(s2.hashCode());//2137211482
//重写hashCode方法后
Student s1  =new Student("张三");
Student s2  =new Student("张三");
System.out.println(s1);//Day06.Student@bd2e9
System.out.println(s2);//Day06.Student@bd2e9
System.out.println(s1.hashCode());//774889
System.out.println(s2.hashCode());//774889

我们看到,重写方法后,相同对象的哈希值是相同的。但是,只比较哈希值不靠谱的,因为有的时候内容不同,哈希值也可能相同。

System.out.println("重地".hashCode());//哈希值:1179395
System.out.println("通话".hashCode());//哈希值:1179395

对于最底层的hashCode来说,哈希值不同,对象肯定不同,但是对象的哈希值相同时,还需要比较其内容。这里也就是用到了equals方法

Object类是所有类的根,它里边的equals()方法是比较地址是否相同的,也就是说原始equals方法是用来比较地址的:

public boolean equals(Object obj) {
     return (this == obj);
 }	

那为什么我们比较字符串的时候是比较内容呢?因为String类中重写了equals()方法,当然此时我是不能完全看明白,只能大概理解这是比较内容是否相同

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String aString = (String)anObject;
        if (coder() == aString.coder()) {
            return isLatin1() ? StringLatin1.equals(value, aString.value)
                              : StringUTF16.equals(value, aString.value);
        }
    }
    return false;
}

结论

Java提供的类是重写了hashCode()和equals()方法是被重写的,我们可以只用拿来用,但是自己定义的类比如Student类,Animal类一定要重写这两个方法。IDEA中是提供了自动生成的方法的,如下

String name;
@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Student student = (Student) o;
    return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
    return name != null ? name.hashCode() : 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值