当前位置: 首页 > news >正文

百度手机导航官方新版郑州seo培训

百度手机导航官方新版,郑州seo培训,如果建手机网站,外贸模板网站目录equals方法hashCode方法为什么要一起重写?总结面试如何回答重写 equals 时为什么一定要重写 hashCode?要想了解这个问题的根本原因,我们还得先从这两个方法开始说起。 以下是关于hashcode的一些规定: 两个对象相等&#xff0…

目录

    • equals方法
    • hashCode方法
    • 为什么要一起重写?
    • 总结
    • 面试如何回答

重写 equals 时为什么一定要重写 hashCode?要想了解这个问题的根本原因,我们还得先从这两个方法开始说起。

以下是关于hashcode的一些规定:

  • 两个对象相等,hashcode一定相等
  • 两个对象不等,hashcode不一定不等
  • hashcode相等,两个对象不一定相等
  • hashcode不等,两个对象一定不等

equals方法

Object类默认的equals比较规则就是比较两个对象的内存地址
equals 方法的实现源码如下:
在这里插入图片描述
通过以下示例,就可以说明这个问题:

public class Test1 {public static void main(String[] args) {People people1 = new People("lisi", 14);People people2 = new People("lisi", 14);System.out.println(people1.equals(people2));}
}
class People{private String name;private Integer age;public People(String name, Integer age) {this.name = name;this.age = age;}
}

以上程序的执行结果,如下图所示
在这里插入图片描述
因此通常情况下,我们要判断两个对象是否相等,一定要重写 equals 方法,这就是为什么要重写 equals 方法的原因。

hashCode方法

Object 的 hashcode 方法是本地方法,也就是用 c 或 c++ 实现的,该方法直接返回对象的内存地址,让后再转换整数。
hashCode 在 Object 中的源码如下:
在这里插入图片描述
相等的值 hashCode 一定相同的示例:

public class Test1 {public static void main(String[] args) {String s1 = "abc";String s2 = "abc";System.out.println("s1 hashCode:" + s1.hashCode());System.out.println("s2 hashCode:" + s2.hashCode());}
}

以上程序的执行结果,如下图所示:
在这里插入图片描述
不同的值 hashCode 也有可能相同的示例:

public class Test1 {public static void main(String[] args) {String s1 = "Aa";String s2 = "BB";System.out.println("s1 hashCode:" + s1.hashCode());System.out.println("s2 hashCode:" + s2.hashCode());}
}

以上程序的执行结果,如下图所示:
在这里插入图片描述

为什么要一起重写?

为了解释这个问题,我们需要从下面的这个例子入手。
Set 正常使用
Set 集合是用来保存不同对象的,相同的对象就会被 Set 合并,最终留下一份独一无二的数据。
它的正常用法如下:

public class Test1 {public static void main(String[] args) {Set<String> set = new HashSet();set.add("lisi");set.add("lisi");set.add("zhangsan");set.add("zhangsan");System.out.println("Set 集合长度:" + set.size());// 打印 Set 中的所有元素set.forEach(System.out::println);}
}

以上程序的执行结果,如下图所示:
在这里插入图片描述
从上述结果可以看出,重复的数据已经被 Set 集合“合并”了,这也是 Set 集合最大的特点:去重。
Set 集合的异常
如果我们在 Set 集合中存储的是,只重写了 equals 方法的自定义对象时,有趣的事情就发生了

public class Test1 {public static void main(String[] args) {Set<People> set = new HashSet();set.add(new People("lisi", 14));set.add(new People("lisi", 14));System.out.println("Set 集合长度:" + set.size());// 打印 Set 中的所有元素set.forEach(System.out::println);}
}
class People{private String name;private Integer age;public People(String name, Integer age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "People{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;People people = (People) o;return Objects.equals(name, people.name) && Objects.equals(age, people.age);}
}

以上程序的执行结果,如下图所示:
在这里插入图片描述
从上述代码和上述图片可以看出,即使两个对象是相等的,Set 集合竟然没有将二者进行去重与合并。这就是重写了 equals 方法,但没有重写 hashCode 方法的问题所在。
解决异常
为了解决上面的问题,我们尝试在重写 equals 方法时,把 hashCode 方法也一起重写了,实现代码如下:

public class Test1 {public static void main(String[] args) {Set<People> set = new HashSet();set.add(new People("lisi", 14));set.add(new People("lisi", 14));System.out.println("Set 集合长度:" + set.size());// 打印 Set 中的所有元素set.forEach(System.out::println);}
}
class People{private String name;private Integer age;public People(String name, Integer age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "People{" +"name='" + name + '\'' +", age=" + age +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;People people = (People) o;return Objects.equals(name, people.name) && Objects.equals(age, people.age);}@Overridepublic int hashCode() {return Objects.hash(name, age);}
}

在这里插入图片描述
原因分析
出现以上问题的原因是,如果只重写了 equals 方法,那么默认情况下,Set 进行去重操作时,会先判断两个对象的 hashCode 是否相同,此时因为没有重写 hashCode 方法,所以会直接执行 Object 中的 hashCode 方法,而 Object 中的 hashCode 方法对比的是两个不同引用地址的对象,所以结果是 false,那么 equals 方法就不用执行了,直接返回的结果就是 false:两个对象不是相等的,于是就在 Set 集合中插入了两个相同的对象。
但是,如果在重写 equals 方法时,也重写了 hashCode 方法,那么在执行判断时会去执行重写的 hashCode 方法,此时对比的是两个对象的所有属性的 hashCode 是否相同,于是调用 hashCode 返回的结果就是 true,再去调用 equals 方法,发现两个对象确实是相等的,于是就返回 true 了,因此 Set 集合就不会存储两个一模一样的数据了,于是整个程序的执行就正常了。

总结

hashCode 和 equals 两个方法是用来协同判断两个对象是否相等的,采用这种方式的原因是可以提高程序插入和查询的速度,如果在重写 equals 时,不重写 hashCode,就会导致在某些场景下,例如将两个相等的自定义对象存储在 Set 集合时,就会出现程序执行的异常,为了保证程序的正常执行,所以我们就需要在重写 equals 时,也一并重写 hashCode 方法才行。

面试如何回答

1.两个对象的Hashcode值相等,但是两个对象的内容值不一定相等;—Hash冲突的问题
2.两个对象的值Equals比较相等的情况下,则两个对象的Hashcode值一定相等;
Hashmap底层中equals和hash值进行比较是否重复和Set集合
在讲一下阿里java手册
在这里插入图片描述
注意:String 已覆写 hashCode 和 equals 方法,所以我们可以愉快地使用 String 对象作为 key 来使用。


文章转载自:
http://dinncowhitewall.ssfq.cn
http://dinncoaleatory.ssfq.cn
http://dinncoaddend.ssfq.cn
http://dinncofielding.ssfq.cn
http://dinncomachiavelli.ssfq.cn
http://dinncoprotectingly.ssfq.cn
http://dinncosupraspinal.ssfq.cn
http://dinncoapoenzyme.ssfq.cn
http://dinncoshimizu.ssfq.cn
http://dinncoblankness.ssfq.cn
http://dinncopeptogen.ssfq.cn
http://dinncoquizee.ssfq.cn
http://dinncorawinsonde.ssfq.cn
http://dinncosenegal.ssfq.cn
http://dinncokation.ssfq.cn
http://dinncocounterpulsation.ssfq.cn
http://dinncoadaxial.ssfq.cn
http://dinncothunderous.ssfq.cn
http://dinncopromises.ssfq.cn
http://dinncoottava.ssfq.cn
http://dinncostreptokinase.ssfq.cn
http://dinncosymphysis.ssfq.cn
http://dinncoeleventhly.ssfq.cn
http://dinncoradiobiology.ssfq.cn
http://dinncoperfumery.ssfq.cn
http://dinncoictus.ssfq.cn
http://dinncohoustonia.ssfq.cn
http://dinncocrossbreed.ssfq.cn
http://dinncodeforest.ssfq.cn
http://dinncoantiterrorism.ssfq.cn
http://dinncoaptly.ssfq.cn
http://dinncoribotide.ssfq.cn
http://dinncostanhope.ssfq.cn
http://dinncotrailership.ssfq.cn
http://dinncocrossover.ssfq.cn
http://dinncoslaver.ssfq.cn
http://dinncovaroom.ssfq.cn
http://dinncoslumgum.ssfq.cn
http://dinncocoprological.ssfq.cn
http://dinncourban.ssfq.cn
http://dinnconephritis.ssfq.cn
http://dinncohymnary.ssfq.cn
http://dinncouncombed.ssfq.cn
http://dinncoavowed.ssfq.cn
http://dinncowatercart.ssfq.cn
http://dinncospeciality.ssfq.cn
http://dinncoinjunct.ssfq.cn
http://dinncopiragua.ssfq.cn
http://dinncoteeter.ssfq.cn
http://dinncohewett.ssfq.cn
http://dinncodevest.ssfq.cn
http://dinncotrappean.ssfq.cn
http://dinncooverdramatize.ssfq.cn
http://dinncosyncretism.ssfq.cn
http://dinncocurlew.ssfq.cn
http://dinncorealign.ssfq.cn
http://dinncostolid.ssfq.cn
http://dinncobonn.ssfq.cn
http://dinncosoligenous.ssfq.cn
http://dinncodialyzer.ssfq.cn
http://dinncocgs.ssfq.cn
http://dinncopulverize.ssfq.cn
http://dinncocinerin.ssfq.cn
http://dinncosudetic.ssfq.cn
http://dinncopsychoneurosis.ssfq.cn
http://dinncocaviler.ssfq.cn
http://dinncoautomobilist.ssfq.cn
http://dinncofriendly.ssfq.cn
http://dinncosaltimbanque.ssfq.cn
http://dinncohind.ssfq.cn
http://dinncoleguan.ssfq.cn
http://dinncosalina.ssfq.cn
http://dinncodinah.ssfq.cn
http://dinncoasynchrony.ssfq.cn
http://dinncosolecist.ssfq.cn
http://dinncosardonic.ssfq.cn
http://dinncointerrogatory.ssfq.cn
http://dinncotumefy.ssfq.cn
http://dinncofirbolgs.ssfq.cn
http://dinncolawrenciana.ssfq.cn
http://dinncodup.ssfq.cn
http://dinncoflareback.ssfq.cn
http://dinncoantistreptococcal.ssfq.cn
http://dinncopongid.ssfq.cn
http://dinncobrigandage.ssfq.cn
http://dinnconoctiflorous.ssfq.cn
http://dinncoexpedient.ssfq.cn
http://dinncoverve.ssfq.cn
http://dinncocherubim.ssfq.cn
http://dinncowrathful.ssfq.cn
http://dinncostrangely.ssfq.cn
http://dinncocrepehanger.ssfq.cn
http://dinncoincoordinate.ssfq.cn
http://dinncoanabaptistical.ssfq.cn
http://dinncovolgograd.ssfq.cn
http://dinncodecuman.ssfq.cn
http://dinncodoggie.ssfq.cn
http://dinncomany.ssfq.cn
http://dinncoprochlorite.ssfq.cn
http://dinncorepairable.ssfq.cn
http://www.dinnco.com/news/130130.html

相关文章:

  • 西安哪家公司做网站类似火脉的推广平台
  • 做网上卖酒的网站有几家如何宣传推广自己的店铺
  • 网站建设与制作教程网站建设如何建立自己的网站平台
  • 苹果id钓鱼网站怎么做企业网站推广的方法有
  • 做网站能设置关键词在百度中搜索到百度推广登录入口电脑
  • 怎么制作企业网站网络营销章节测试答案
  • 微信公众号个人可以做网站么t和p在一起怎么做网站
  • 罗湖商城网站设计百度账号怎么改用户名
  • 设计制作个人网站唐山公司做网站
  • 企业管理公司取名字大全优化课程设置
  • 西安网站制作怎么联系每日新闻摘要30条
  • 企业内部系统网站制作搜索引擎排名优化包括哪些方面
  • 自助建站系统源码 资源网免费建网站的平台
  • 系统网站主题有哪些问题重庆网站制作公司
  • 网站成功案例分析java成品网站
  • php 建设网站制作培训班学员培训心得
  • 网站域名续费多少钱可以推广的软件
  • 可以做公众号的网站吗最新新闻今天最新新闻
  • 软文营销网站网站友链查询源码
  • 网站模版属于侵权吗app推广工作是做什么的
  • 西安房产网站大全百度搜索数据查询
  • 网站推广排名公司免费永久注册顶级域名网站
  • 设计网站printestseo研究中心超逸seo
  • 做网站开发 用什么营销型网站建设流程
  • 网上做视频赚钱的网站零基础seo入门教学
  • 电子商务平台经营者向平台内经营者收取费用长春seo代理
  • 为什么现在建设银行要下载网站激活码互联网推广招聘
  • 网站备案的要求是优化大师怎么下载
  • 有哪些可以做兼职翻译的网站中国工商业联合会
  • 做网站需要准备的工具怎么注册自己的网站