java中的equals方法+hashCode方法

【0】README

0.1)以下内容均为原创,包括源代码, 旨在理清 equals 和 hashCode 方法的 实现原理
0.2) for full resource code, please visit
https://github.com/pacosonTang/core-java-volume/blob/master/chapter13/EqualsHashCodeTest.java

【1】equals 方法

1.1)父类Object中的equals 方法, 它本身比较的就是两个对象的内存地址是否相等(Object.equals 源码):
这里写图片描述
1.2)自定义类中,如果希望对象中各个成员变量 分别相等,则它们的equals 方法 返回 true, 则:
这里写图片描述
这里写图片描述


【2】hashCode 方法(String.hashCode 源码)

这里写图片描述
2.1)而 Object.hashCode 的返回值:
2.2)以下内容转自 API:http://docs.oracle.com/javase/8/docs/api/

The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
  • 2.2.1)译文如下:
  • hashCode的常规约定是:
    • 1)对同一个对象的不止一次调用, hashCode 方法必须返回同一个整型值,假如 在equals方法 比较两个对象的过程中 没有信息被修改的话。当对同一个应用的不同执行,这个值(hashCode)不需要保持一致;可以不一样;
    • 2)如果两个对象在 equals 方法中被认为相等, 则 上述两个对象的 hashCode 也应该返回相同的值;
    • 3)不做要求的是,如果 java.lang.Object.equals 认为两个对象不相等,则 两个对象的 hashCode 方法 返回 不同的值。然而, 编码者应该注意的是,对不相等对象产生的不同整型值(hashCode) 可能会影响 哈希表的性能;
  • Object.hashCode 的返回值:(对象的内部地址值)
    • 要做到尽可能合理实用, Object.hashCode 方法对不同的对象产生不同的整型值。(这往往是通过 将对象的内部地址值转换为 整型值来实现的, 但这个实现技术不是 Java 编程语言硬性要求的)。

2.3) 我们再来看 Object.hashCode 的源码:
这里写图片描述
for detailed native keyword , please visit http://blog.csdn.net/pacosonswjtu/article/details/50312333
这里写图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331747.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql判断条件用法,MySQL数据库讲解条件判断函数 MySQL数据库使用教程

函数&#xff1a;(1)IF(expr,v1,v2)函数(2)IFNULL(v1,v2)函数(3)CASE函数(相关免费学习推荐&#xff1a;mysql视频教程)(1)if(expr,v1,v2)函数在if(expr,v1,v2)函数中,若表达式expr是true(expr<>0 and epr<>null)返回v1&#xff0c;否则返回v2。【例】使用if()函数…

cuba.platform_CUBA 7.2 –有什么新功能?

cuba.platformCUBA平台的第七版向前迈出了一大步。 内部体系结构的改进和新的IDE为进一步改进奠定了良好的基础。 我们将继续添加新功能&#xff0c;以使开发人员的生活更轻松&#xff0c;并使他们的工作更加高效。 在7.2版中&#xff0c;我们引入了许多可能看起来像是主要更新…

javafx之TableView的TableColumn

TableColumn列 列与Bean之间建立联系&#xff1a; setCellValueFactory(); 通过cell值工厂建立与Bean的联系。它这里并不需要知道你是传了什么Bean&#xff0c;它只需要通过“字段名”反射去Bean里面获得值&#xff0c;所以Bean属性定义的名字不需要与它相同&#xff0c;只需…

java集合——具体的集合

【0】README 0.1&#xff09; 本文描述 转自 core java volume 1&#xff0c; 旨在理解 java集合——具体的集合 的相关知识&#xff1b; 【1】下表展示了 java类库中的集合&#xff0c;并简要描述了每个集合类的用途。 1.1&#xff09;在下表中&#xff0c; 除了以 Map结尾的…

excel 26进制 php,记录一次华为招聘的编程题-excel中的26进制

var line "abcdefghijklmnopqrstuvwxyz";var list line.split("");function baseConversion(N) {var jz [];//获得有0的26进制while (true) {if (parseInt(N/26) 0) {jz.push(N%26);break;} else{jz.push(N%26);N parseInt(N/26);}}//转化成无0的26进…

日发帖 发帖频率 发帖时段_先发帖

日发帖 发帖频率 发帖时段通常&#xff0c;我们编写代码来计算出一堆可用的答案。 让我们来看一下Java中的情况。 public Widget getAppropriateWidget(CustomerRequest request) { if (shelfstock.contains(request.getBarcode()) { return new ShelfWidget(); } if (backroom…

java集合——数组列表(ArrayList)+散列集(HashSet)

【0】README 0.1&#xff09; 本文描述源代码均 转自 core java volume 1&#xff0c; 旨在理解 java集合——数组列表&#xff08;ArrayList&#xff09;散列集&#xff08;HashSet&#xff09; 的相关知识&#xff1b; 0.2&#xff09; 散列集HashSet 涉及到 hashCode&…

javafx之TableView的TaleCell

TaleCell 对TableColumn的cell里面弄重新构造 TableColumn的setCellFactory(TextFieldTableCell.forTableColumn());有一些默认的构造。 或者重写TableCell类 [java] view plaincopy tableColumn.setCellFactory(new Callback<TableColumn<Path, Number>, TableCell…

jdk8 cms g1gc_JDK 14:CMS GC是OBE

jdk8 cms g1gcJDK 14 Early Access Build&#xff03;23 &#xff08; 2019/11/13 &#xff09; 现已上市 。 此版本最值得注意的更改之一是删除了并发标记扫描垃圾收集器 。 JEP 291 [“弃用并发标记扫描&#xff08;CMS&#xff09;垃圾收集器”]早在2017年就使用JDK 9和JEP …

java集合——树集(TreeSet)+对象的比较

【0】README 0.1&#xff09; 本文描述转自 core java volume 1&#xff0c; 源代码为原创&#xff0c;旨在理解 java集合——树集&#xff08;TreeSet&#xff09;对象的比较 的相关知识&#xff1b; 0.2&#xff09; for full source code, please visit https://github.co…

php curl伪装cookies,php curl 添加cookie伪造登陆抓取数据

有的网页必须登陆才能看到&#xff0c;这个时候想要抓取信息必须在header里面传递cookie值才能获取1、首先登陆网站&#xff0c;打开firebug就能看到对应的cookie把这些cookie拷贝出来就能使用了2、<?php header("Content-type:text/html;Charsetutf8");$ch curl…

JavaFX之TableView的TableRow

TableRow 通过TableView的setRowFactory,对行的双击进行操作 tableView.setRowFactory(new Callback<TableView<T>, TableRow<T>>() { Override public TableRow<T> call(TableView<T> param) { return new TableRowControl(); }…

rpc结构错误_结构性错误

rpc结构错误团队成员在使用以下代码时遇到了麻烦&#xff1a; void extractData(String targetUri) { Path tempFile createTempFilePath(); extractDataToPathAndUpload(tempFile, targetUri); cleanUp(tempFile); } void extractDataToPathAndUpload(Path tempFile, Stri…

zabbix前端php界面,Zabbix Web UI

PS&#xff1a;其实安装zabbix很简单&#xff0c;网上资料一搜一大把&#xff0c;基本都是采用yum方式安装&#xff0c;简单省事。正因如此我没写相关配置文档&#xff0c;安装方式一般用yum安装或源码安装亦或是容器安装&#xff0c;最近有网友提出来了&#xff0c;我认真对待…

Comparable与Comparator的区别

【0】README 0.1&#xff09;本文章节一&#xff08;除开 Conclusion内容&#xff09;转自&#xff1a; http://blog.csdn.net/mageshuai/article/details/3849143 0.2&#xff09; 余下内容均为原创&#xff0c;包括源代码&#xff1b; 【1】Comparable与Comparator的区别&a…

JavaFX之TableView的MenuButton

MenuButton 在JavaFx的 TableView 上可以使用MenuButton来管理TableView的Column. 启用MenuButton. TableView.setTableMenuButtonVisible(true); 但是普通的MenuButton, 每次点击都会刷新TableView 重写TableMenuButton的事件 [java] view plaincopy 找到tableView 的 …

java 读取 文本块_Java 13:文本块

java 读取 文本块Java 13已交付了期待已久的多行字符串或Text Blocks 。 您不再需要连接跨越多行的字符串或转义特殊字符&#xff0c;这确实提高了代码的可读性。 文本块是一种预览语言功能 &#xff0c;这意味着必须使用--enable-preview标志在Java编译器和运行时中明确启用它…

php 崩溃 输出,php – 创建可崩溃的print_r()var_dump()的函数?

你可能熟悉print_r的这个输出&#xff1a;Hierarchy Object([parent:private] > Hierarchy Object([parent:private] >[children:private] > Array()[level:private] > 0[price_modes:private] > Array()[id:protected] >[left_id:protected] >[right_id:…

JavaFX之TableView的SelectionMode

SelectionMode table默认是只能选着一行的&#xff0c;如果想选着多行&#xff0c;设置SelectionMode,此时可以对选中的多个进行监听。 [java] view plaincopy ListChangeListener<Person> indicesListener new ListChangeListener<Person>() { …

Comparable and Comparator API

【0】README 0.1&#xff09;文本全文翻译于 java 8 JDK 官方文档中关于 Comparable and Comparator 的描述&#xff1b; 0.2&#xff09;能力有限&#xff0c;仅供参考&#xff1b; 【1】 Interface Comparable 1.0&#xff09;类型参数&#xff1a; 与本对象进行比较的另一…