当前位置:网站首页>equals提高执行速度/性能优化
equals提高执行速度/性能优化
2022-06-29 10:37:00 【努 力 小 子】
原理上与布隆过滤器接近。
当把对象加入 HashSet时,HashSet 会先计算对象的 hashcode 值来判断对象加入的位置,同时也会与该位置其他已经加入的对象的 hashcode 值作比较,如果没有相符的 hashcode,HashSet 会假设对象没有重复出现。但是如果发现有相同 hashcode 值的对象,这时会调用 equals() 方法来检查 hashcode 相等的对象是否真的相同。如果两者相同,HashSet 就不会让其加入操作成功。如果不同的话,就会重新散列到其他位置。
public class test1 {
public static void main(String[] args) {
String a = new String("ab"); // a 为一个引用
String b = new String("ab"); // b为另一个引用,对象的内容一样
String aa = "ab"; // 放在常量池中
String bb = "ab"; // 从常量池中查找
if (aa == bb) // true
System.out.println("aa==bb");
if (a == b) // false,非同一对象
System.out.println("a==b");
if (a.equals(b)) // true
System.out.println("aEQb");
if (42 == 42.0) // true
System.out.println("true");
}
}
这样就大大减少了 equals 的次数,相应就大大提高了执行速度。通过我们可以看出: hashCode() 的作用就是获取哈希码,也称为散列码;它实际上是返回一个 int 整数。这个哈希码的作用是确定该对象在哈希表中的索引位置。hashCode() 在散列表中才有用,在其它情况下没用。在散列表中 hashCode() 的作用是获取对象的散列码,进而确定该对象在散列表中的位置。
边栏推荐
- 【HBZ分享】Semaphore 与 CountDownLatch原理
- QT learning 11 string classes in QT
- 多线程高并发服务器:3个问题
- Doodle cloud development demo login
- 什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手
- 【HBZ分享】Mysql的InnoDB原理
- 海泰前沿技术|隐私计算技术在医疗数据保护中的应用
- MySQL开启慢查询
- Babbitt | yuancosmos daily must read: HTC announced the launch of the first yuancosmos mobile phone, which costs about 2700 yuan. What are the new ways to play
- Week 12 experiment -- implementation of VGA protocol based on FPGA
猜你喜欢

9 easy to use JSON processing tools, greatly improving efficiency!

什么?漫画居然能免费看全本了,这还不学起来一起做省钱小能手

Modbustcp protocol WiFi wireless learning single channel infrared module (round shell version)

Modbustcp protocol WiFi wireless learning single channel infrared module (small shell version)

After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell

又拍雲 Redis 的改進之路

Spark - one to one correspondence between task and partition and detailed explanation of parameters

Qt学习09 计算器界面代码重构

中科方德技术专家直播:如何基于 OpenStack、Ceph 构建私有云平台? | 第 27 期

CTO专访:合见工软深化产品布局 加速国产EDA技术革新
随机推荐
The first "cyborg" in the world died, and he only transformed himself to "change his life against the sky"
(JS) status mode
Specific method and example program of Siemens s7-200smart control stepping motor
Today in history: musk was born; Microsoft launches office 365; The inventor of Chua's circuit was born
(JS) pure function in array
LeetCode 535 TinyURL的加密与解密[map] HERODING的LeetCode之路
When a technician becomes a CEO, what "bugs" should be modified?
Nuc980 open source project 16- start from SPI flash (w25q128)
How to identify the exact length and width of the contour
Modbustcp protocol network learning single channel infrared module (medium shell version)
Multithreaded high concurrency server: three problems
The use of Fibonacci sequence and bubble sort in C language
适合小白的树莓派opencv4.0安装
稳定币风险状况:USDT 和 USDC 安全吗?
Necessary for cloud native development: the first common codeless development platform IVX editor
What are the pop, push, unshift, and shift of the (JS) array?
Doodle cloud development demo login
Modbustcp protocol network learning single channel infrared module (double-layer board)
Unity学习笔记--Vector3怎么设置默认参数
Pipeline aggregations pipeline aggregation-sibling-1