当前位置:网站首页>图解HashCode存在的意义
图解HashCode存在的意义
2022-04-23 05:42:00 【ShuangTwo】
图解HashCode存在的意义
HashCode存在的意义:减少equals比较次数,提高执行速读。
对象加入HashSet时HashCode就起到了这样的作用。
下面是关于“HashSet检查重复”的描述,来源于《Headfirst java》第二版。
当你把对象加入 HashSet 时,HashSet 会先计算对象的 hashcode 值来判断对象加入的位置,同时也会与其他已经加入的对象的 hashcode 值作比较,如果没有相符的 hashcode,HashSet 会假设对象没有重复出现。但是如果发现有相同 hashcode 值的对象,这时会调用 equals()方法来检查 hashcode 相等的对象是否真的相同。如果两者相同,HashSet 就不会让其加入操作成功。如果不同的话,就会重新散列到其他位置。(摘自我的 Java 启蒙书《Headfirst java》第二版)。这样我们就大大减少了 equals 的次数,相应就大大提高了执行速度。
整理的图解,希望帮助大家理解。
HashCode起到的作用,比较加入对象的HashCode与已经加入对象的HashCode,重要的一步是当比较不同时直接将对象加入散列表,不需使用equals比较。根据我们的经验,加入的对象绝大多数都是不存在,这样一来实际上用到equals比较的情况就减少了,性能得以提升。
版权声明
本文为[ShuangTwo]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43356538/article/details/123874651
边栏推荐
- 2-软件设计原则
- JVM系列(4)——内存溢出(OOM)
- Markdown syntax support test
- Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
- Strategies to improve Facebook's touch rate and interaction rate | intelligent customer service helps you grasp users' hearts
- Redis经典面试题总结2022
- Issue 36 summary of atcoder beginer contest 248
- Pavlov and hobbies
- PreparedStatement防止SQL注入
- Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
猜你喜欢
Issue 36 summary of atcoder beginer contest 248
Radar equipment (greedy)
2 - software design principles
C, class library
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
2 - principes de conception de logiciels
Package mall system based on SSM
基于ssm 包包商城系统
Interview Basics
SQL statement simple optimization
随机推荐
框架解析2.源码-登录认证
实体中list属性为空或者null,设置为空数组
基于ssm 包包商城系统
Xiuxian real world and game world
shell指令学习1
Transposed convolution
热键,界面可视化配置(界面交互)
Frequently asked interview questions - 1 (non technical)
[machine learning] scikit learn introduction
Character recognition easyocr
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
JDBC操作事务
创建线程的三种方式
Parameter analysis of open3d material setting
Breadth first search topics (BFS)
jdbc入门\获取数据库连接\使用PreparedStatement
MySQL lock mechanism
Getting started with JDBC \ getting a database connection \ using Preparedstatement
SQL语句简单优化
Flutter 新一代圖形渲染器 Impeller