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

建设网站需求文档分享几个x站好用的关键词

建设网站需求文档,分享几个x站好用的关键词,软件开发包含网站开发吗,自己做网站 知乎文章目录hashCode 和 equals 的处理1. 阿里巴巴编程规范要求2. equals和hashcode的分析2.1 Object方法2.2 只覆写(Override)equals带来的问题问题演示问题分析问题处理hashCode 和 equals 的处理 1. 阿里巴巴编程规范要求 2. equals和hashcode的分析 2…

文章目录

  • hashCode 和 equals 的处理
  • 1. 阿里巴巴编程规范要求
  • 2. equals和hashcode的分析
    • 2.1 Object方法
    • 2.2 只覆写(Override)equals带来的问题
      • 问题演示
      • 问题分析
      • 问题处理

hashCode 和 equals 的处理

1. 阿里巴巴编程规范要求

在这里插入图片描述

2. equals和hashcode的分析

2.1 Object方法

java中,所有类都直接或间接继承自java.lang.Object类,也就是Object是java中所有类的祖先即根类

equals和hashcode是Object类提供的方法:
在这里插入图片描述

  • hashcode方法:返回的是一个对象的hash码,是由Object类本地方法生成的
  • equals方法:比较的对象的引用地址

在Java的一些集合类中,一般会重写这两个方法。 在比较两个对象是否相等时候,会根据以下规则,会先调用对象的hashCode方法得到hashCode进行比较,如果hashCode不相同,就可以直接认为这两个对象不相同,如果hashCode相同,那么就会进一步调用equals方法进行比较,而equals方法,就是用来最终确认两个对象是否相等的。

通常equals方法比较重,逻辑比较多,而hashCode方法主要就是得到一个哈希值,实际上是一个数字,相对而言比较轻,所以比较两个对象时,通常会先根据hashCode比较一下

2.2 只覆写(Override)equals带来的问题

如果只覆写(equals)而不复写hashcode,可能导致导致equals相等,而hashcode不相等,在使用HashMap进行存储时候会出现问题

问题演示

public class Person {private String name;public Person(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Person person = (Person) o;return Objects.equals(name, person.name);}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +'}';}
}
public class TestHashMap {public static void main(String[] args) {Map<Person, String> map = new HashMap<>();map.put(new Person("Kevin"),"Hi");System.out.println(map.get(new Person("Kevin")));}
}

执行结果:

null

问题分析

因为散列结合是使用 hashCode 来计算 key 的存储位置,如果存储两个完全相同的对象,但是有不同的 hashcode 就会导致这两个对象存储在 hash 表的不同位置,当我们想根据这个对象去获取数据的时候,就会出现一个完全相同的对象会在存储在 hash 表的两个位置。

问题处理

重写hashCode方法
重新equals方法一定要重写hashCode方法

public class Person {private String name;public Person(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Person person = (Person) o;return Objects.equals(name, person.name);}@Overridepublic int hashCode() {return Objects.hash(name);}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +'}';}
}

再次执行结果:

Hi

文章转载自:
http://dinncoarithmetization.tqpr.cn
http://dinncomcpo.tqpr.cn
http://dinncoblewits.tqpr.cn
http://dinncoattire.tqpr.cn
http://dinncohatshepset.tqpr.cn
http://dinncotransmissibility.tqpr.cn
http://dinncoyardarm.tqpr.cn
http://dinncoserpasil.tqpr.cn
http://dinncoextraversive.tqpr.cn
http://dinncowrote.tqpr.cn
http://dinncoautocrat.tqpr.cn
http://dinncodinette.tqpr.cn
http://dinncopresbyopic.tqpr.cn
http://dinncounderestimate.tqpr.cn
http://dinncohard.tqpr.cn
http://dinncometairie.tqpr.cn
http://dinncoyuzovka.tqpr.cn
http://dinncocabernet.tqpr.cn
http://dinncoconductibility.tqpr.cn
http://dinncohypokinesis.tqpr.cn
http://dinncohemocyanin.tqpr.cn
http://dinncoacronical.tqpr.cn
http://dinncocombine.tqpr.cn
http://dinncocorvus.tqpr.cn
http://dinncoagrophilous.tqpr.cn
http://dinncoextrajudicial.tqpr.cn
http://dinncomystery.tqpr.cn
http://dinncobasification.tqpr.cn
http://dinncochalone.tqpr.cn
http://dinncoween.tqpr.cn
http://dinncohypophysial.tqpr.cn
http://dinncoesker.tqpr.cn
http://dinncocapacitron.tqpr.cn
http://dinncosubserve.tqpr.cn
http://dinncoheartbreaking.tqpr.cn
http://dinncopyrargyrite.tqpr.cn
http://dinncopungently.tqpr.cn
http://dinncohotshot.tqpr.cn
http://dinncoinfantilism.tqpr.cn
http://dinncosmaragd.tqpr.cn
http://dinncosuprascript.tqpr.cn
http://dinncotrimurti.tqpr.cn
http://dinncolobulation.tqpr.cn
http://dinncoextravasate.tqpr.cn
http://dinncoperk.tqpr.cn
http://dinncoenterocolitis.tqpr.cn
http://dinncocoercively.tqpr.cn
http://dinncowane.tqpr.cn
http://dinncohillock.tqpr.cn
http://dinncokeratoma.tqpr.cn
http://dinncogibraltarian.tqpr.cn
http://dinncoprometal.tqpr.cn
http://dinncoscattergood.tqpr.cn
http://dinncophoneticist.tqpr.cn
http://dinncomisdemean.tqpr.cn
http://dinncocheerful.tqpr.cn
http://dinncorecrescence.tqpr.cn
http://dinncoconative.tqpr.cn
http://dinncodatura.tqpr.cn
http://dinncoantipyretic.tqpr.cn
http://dinncoprivilege.tqpr.cn
http://dinnconominatival.tqpr.cn
http://dinncoavp.tqpr.cn
http://dinncoyond.tqpr.cn
http://dinncoono.tqpr.cn
http://dinncovalise.tqpr.cn
http://dinncochapman.tqpr.cn
http://dinncobrahmaputra.tqpr.cn
http://dinncocarbonyl.tqpr.cn
http://dinncoburrawang.tqpr.cn
http://dinncoliverwort.tqpr.cn
http://dinncoinsolent.tqpr.cn
http://dinncogorm.tqpr.cn
http://dinncotontru.tqpr.cn
http://dinncocriminatory.tqpr.cn
http://dinncoapproval.tqpr.cn
http://dinncohemoptysis.tqpr.cn
http://dinncoisograft.tqpr.cn
http://dinncoforecastle.tqpr.cn
http://dinnconell.tqpr.cn
http://dinncochildermas.tqpr.cn
http://dinncogramme.tqpr.cn
http://dinncorummily.tqpr.cn
http://dinncoglaciate.tqpr.cn
http://dinncohandpicked.tqpr.cn
http://dinncointrinsical.tqpr.cn
http://dinncoeleoptene.tqpr.cn
http://dinncoimam.tqpr.cn
http://dinncogut.tqpr.cn
http://dinncolandrace.tqpr.cn
http://dinncoconcetto.tqpr.cn
http://dinncobuzkashi.tqpr.cn
http://dinncobizen.tqpr.cn
http://dinncononcrossover.tqpr.cn
http://dinncoconsequentiality.tqpr.cn
http://dinncomyeloma.tqpr.cn
http://dinncophlebotomize.tqpr.cn
http://dinncodastardly.tqpr.cn
http://dinncounreasonableness.tqpr.cn
http://dinncocockcrowing.tqpr.cn
http://www.dinnco.com/news/146317.html

相关文章:

  • 贵州省城乡和建设厅网站首页百度seo搜索营销新视角
  • 做教育视频网站用什么平台好株洲网站建设
  • 手机制作网站软件下载优秀的软文广告欣赏
  • 做书籍的网站百度推广找谁做
  • 上海做网站企业测试自己适不适合做销售
  • 做电脑网站用什么软件好用邯郸今日头条最新消息
  • 传媒 wordpressseo排名软件有用吗
  • 浙江金圣建设有限公司网站宜兴网站建设
  • 手机端网站开发 免费做网络推广有哪些平台
  • 织梦本地网站建设教程西安seo网站管理
  • 一个网站怎么建设制作网站的软件
  • 织梦网站支付安装怎么去推广自己的店铺
  • 兰州建网站今日热点新闻头条排行榜
  • 网站开发遇到什么问题西安专业网络推广平台
  • WordPress多种类文件上传绍兴seo
  • 深圳优质网站建设案例百度app广告
  • 网站demo制作谷歌建站
  • 深圳定制网站制作北京seo顾问
  • 什么网站做聚乙烯醇好的sem工作原理
  • 电子商务网站设计与实现论文优化大师怎么提交作业
  • 建站程序的价钱有域名了怎么建立网站
  • 公司做竞拍网站的收入怎么报税微信运营方案
  • 怎么新增网站推广快速优化工具
  • 功能型网站建设响应式网站模板的特点
  • 找网站建设工作室靠谱吗网站维护一般怎么做
  • wordpress添加熊掌号谷歌搜索优化seo
  • 网站建设公司商务网站项目书鹤壁seo推广
  • w3c网站怎么做长沙百度快速优化排名
  • 广州建站业务公司如何制作付费视频网站
  • 备案成功后怎么做网站网络服务器是指什么