当前位置:网站首页>说说对hashcode和equals方法的理解?
说说对hashcode和equals方法的理解?
2022-07-25 13:10:00 【紫乾2014】
一、作用
hashCode() 的作用是获取哈希码,也称为散列码;它实际上是返回一个int整数。这个哈希码的作用是确定该对象在哈希表中的索引位置。
equals它的作用是判断两个对象是否相等,如果对象重写了equals()方法,比较两个对象的内容是否相等;如果没有重写,比较两个对象的地址是否相同,等价于“==”。
二、用法
当向哈希表中存放元素时,会先调用对象的hashCode方法,算出对象在哈希表中的存放位置,如果该位置没有元素则直接存入,如果该位置有元素则说明存在哈希冲突,这时会调用对象的equals方法,比较这两个对象是不是同一个对象,如果equals方法返回的是true,那么就不会把第二个对象存放到哈希表,如果返回的是false,就会存入。
边栏推荐
- Excel录制宏
- 为提高效率使用ParallelStream竟出现各种问题
- 基于百问网IMX6ULL_PRO开发板驱动AP3216实验
- 错误: 找不到或无法加载主类 xxxx
- 【CSDN 年终总结】结束与开始,一直在路上—— “1+1=王”的2021总结
- [figure attack and Defense] backdoor attacks to graph neural networks (sacmat '21)
- Common operations for Yum and VIM
- 7行代码让B站崩溃3小时,竟因“一个诡计多端的0”
- ECCV 2022 | climb to the top semantickitti! Semantic segmentation of LIDAR point cloud based on two-dimensional prior assistance
- Design and principle of thread pool
猜你喜欢

Common operations for Yum and VIM

Design and principle of thread pool

录制和剪辑视频,如何解决占用空间过大的问题?

Generate SQL script file by initializing the latest warehousing time of vehicle attributes

卷积神经网络模型之——AlexNet网络结构与代码实现

Shell常用脚本:获取网卡IP地址

Any time, any place, super detective, seriously handle the case!

G027-OP-INS-RHEL-04 RedHat OpenStack 创建自定义的QCOW2格式镜像

Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets

Seven lines of code made station B crash for three hours, but "a scheming 0"
随机推荐
【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
错误: 找不到或无法加载主类 xxxx
7行代码让B站崩溃3小时,竟因“一个诡计多端的0”
详解浮点数的精度问题
EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
Date and time function of MySQL function summary
【历史上的今天】7 月 25 日:IBM 获得了第一项专利;Verizon 收购雅虎;亚马逊发布 Fire Phone
Docekr learning - MySQL 8 master-slave replication setup deployment
[300 opencv routines] 239. accurate positioning of Harris corner detection (cornersubpix)
基于百问网IMX6ULL_PRO开发板移植LCD多点触摸驱动(GT911)
Convolutional neural network model -- vgg-16 network structure and code implementation
yum和vim须掌握的常用操作
Memory layout of program
Emqx cloud update: more parameters are added to log analysis, which makes monitoring, operation and maintenance easier
工业互联网的内涵及其应用
迁移PaloAlto HA高可用防火墙到Panorama
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
0720RHCSA
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
MLX90640 红外热成像仪测温传感器模块开发笔记(五)