为什么重写equals必须重写hoshCode的基础分析
为什么重写equals必须重写hashCode的基础分析
1.我们先来了解下原生的equals和hashCode代码
原生equals:它判断的是两个对象是否相等
原生hashCode值:它是根据内存地址换算出来的一个整数类型的值
2.至于为什么要重写equals和hashCode?
当然为了满足我们具体的业务需求啦,毕竟我们不一定只比较对象相等嘛
3.做一个超简单小案例来理解下(包名不规范,切勿模仿);
(1)创建一个Student类,不重写equals和hashCode
package 重写equal必须重写hashcode;
public class Student {
public String id; //学生的id
public String name; //学生的名字
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String toString() {
return id + ":" + name;
}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
}
(2)new一个hashSet,分别添加三个student
package 重写equal必须重写hashcode;
import java.util.HashSet;
public class Test {
//学生ID和姓名都相同我们视为重复
public static void main(String[] args) {
HashSet hs = new HashSet();
hs.add(new Student("001","小明"));
hs.add(new Student("002","小花"));
hs.add(new Student("002","小花"));
System.out.println(hs);
}
}
(3)运行结果如下:

(4)我们可以看到,信息出现了重复;new的对象不同生成的hashCode值不同,所以hashSet会把三个Student对象当作不同的对象。
2.接下来我们重写equals而不重写hashCode
(1)重写equals后代码如下:
package 重写equal必须重写hashcode;
public class Student {
public String id; //学生的id
public String name; //学生的名字
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String toString() {
return id + ":" + name;
}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
public boolean equals(Object obj) {
if(this == obj) { //判断是否是同一对象
return true; //同一类型返回true(跟自己比较)
}
if(getClass()!=obj.getClass()) { //判断是否为同一类型
return false; //不是同类型返回false(类型不同肯定为不同的对象)
}
Student stu = (Student)obj; //强制转换成Student类型
boolean result = this.id.equals(stu.id); //判断ID是否相等
return result; //返回判断结果
}
}
(2)现在我们运行下,结果如下图:

(3)可以发现重复的信息没有删除掉,可以判断添加Student对象没有调用equals方法,而是根据hashCode值的不同,hashSet就认为三个对象不相等。
3.最后我们重写equals和hashCode;
(1)重写hashCode;
package 重写equal必须重写hashcode;
public class Student {
public String id; //学生的id
public String name; //学生的名字
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String toString() {
return id + ":" + name;
}
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
public boolean equals(Object obj) {
if(this == obj) { //判断是否是同一对象
return true; //同一类型返回true(跟自己比较)
}
if(getClass()!=obj.getClass()) { //判断是否为同一类型
return false; //不是同类型返回false(类型不同肯定为不同的对象)
}
Student stu = (Student)obj; //强制转换成Student类型
boolean result = this.id.equals(stu.id); //判断ID是否相等
return result; //返回判断结果
}
public int hashCode() { //重写hashCode
return id.hashCode(); //返回ID属性的哈希值
}
}
(2)运行结果如下:

(3)是不是就把重复的信息移除了呢?哈哈
如果有不妥之处,请各位大佬多多包涵,这些也是个人的理解
如果你有补充,欢迎留下你的意见在评论区!
为什么重写equals必须重写hoshCode的基础分析的更多相关文章
- 为什么重写equals还要重写hashcode??
equals和hashcode是object类下一个重要的方法,而object类是所有类的父类,所以所有的类都有这两个方法 equals和hashcode间的关系: 1.如果两个对象相同(即equal ...
- 为什么重写equals必须重写hashCode
目录 equals常见面试题 为什么要重写equals 重写equals不重写hashCode会存在什么问题 总结 equals常见面试题 在开始聊之前,我们先看几个常见的面试题,看看你能不能都回答上 ...
- JAVA 重写equals和重写hashCode
面试官可能会问你:“你重写过 hashcode 和 equals 么,为什么重写equals时必须重写hashCode方法?” 首先你需要了解: hashCode()的作用是获取哈希码(散列码) 它实 ...
- 【Java基础】重写equals需要重写hashcode
Object里的equals用来比较两个对象的相等性,一般情况下,当重写这个方法时,通常有必要也重写hashcode,以维护hashcode方法的常规协定,或者说这是JDK的规范,该协定声明相等对象必 ...
- 为什么重写equals还要重写hashcode
参考回答: HashMap中,如果要比较key是否相等,要同时使用这两个函数!因为自定义的类的hashcode()方法继承于Object类,其hashcode码为默认的内存地址,这样即便有相同含义的两 ...
- 谈谈HashSet的存储原理及为什么重写equals必须重写hashcode方法
HashSet的存储原理: 1.将要传入的数据根据系统的hash算法得到一个hash值: 2.根据hash值可以得出该数据在hash表中的位置: 3.判断该位置上是否有值,没有值则把数据插入进来:如果 ...
- 关于重写equals同时重写hashcode
1.Object中equals方法和hashcode public boolean equals(Object obj) { return (this == obj); } public native ...
- 为什么重写equals()就要重写hashcode()
阿里巴巴开发规范 只要重写 equals,就必须重写 hashCode 因为 Set 存储的是不重复的对象,依据 hashCode 和 equals 进行判断,所以 Set 存储的对象必须重写这两个方 ...
- 为什么重写equals()必须重写hashCode()
主要原因是因为hashCode是用对象的内部地址转换成一个整数的,而equals比较得是两个对象,或者是两个对象的内容 如果你重写了equals,而保留hashCode的实现不变,那么很可能两个对象明 ...
随机推荐
- MOOC 数据库系统笔记(一):初步认识数据库系统
概述 什么是数据库 数据库是电子化信息的集合 数据库起源于规范化"表(Table)"的处理. Table:以按行按列形式组织及展现的数据. E.F.Codd,基于对"表( ...
- Java多线程基础知识例子
一.管理 1.创建线程 Thread public class Main { public static void main(String[] args) { MyThread myThread = ...
- .NetCore技术研究-一套代码同时支持.NET Framework和.NET Core
在.NET Core的迁移过程中,我们将原有的.NET Framework代码迁移到.NET Core.如果线上只有一个小型的应用还好,迁移升级完成后,只需要维护.NET Core这个版本的代码. 但 ...
- Android开发——Kotlin开发APP使用笔记
之前一直使用java来开发Android项目,学了新的kotlin语言,前来试一试,并说一下kotlin对Android的一些功能增强 创建项目 我使用的是Android Studio3.0+,所以默 ...
- 【博客美化】添加github图标
<a href="https://github.com/cai3231" target="_blank"> <img style=" ...
- Scala 学习笔记之函数(2)
class OldStudent extends Student { def filterName(s: String, f: String => String) = { if (s != nu ...
- FFmpeg(一)
1. FFmpeg分为3个版本:Static. Shared. Dev 前两个版本可以直接在命令行中使用.包含了三个exe:ffmpeg.exe,ffplay.exe,ffprobe.exe Sta ...
- JavaSE----03.数据类型
03.数据类型 1.数据类型分类 Java是强类型语言,Java中的数据类型分为两大类,分别是基本数据类型和引用数据类型.其中,基本数据类型由Java语言定义,其数据占用内存的大小固定,在内存 ...
- 设计模式----行为型模式之观察者模式(Observer Pattern)
下面是阅读<Head First设计模式>的笔记. 观察者模式 定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新. JDK API内置机制 ...
- 做高逼格程序员之说走就走的「Windows」
简介:随着移动固态硬盘越来越便宜,网上逐渐出来一个黑科技.Windows To GO见名知意.简单来说就是在U盘或者是移动固态硬盘上安装Windows系统.达到即插即用. WTG 简介 Windows ...