当前位置:网站首页>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() 的作用是获取对象的散列码,进而确定该对象在散列表中的位置。
边栏推荐
- How to find out the wrong mobile number when querying MySQL
- misc3~7
- (JS) filter out keys with value greater than 2 in the object
- Leetcode 535 encryption and decryption of tinyurl [map] the leetcode road of heroding
- 斐波那锲数列与冒泡排序法在C语言中的用法
- 2.8万字的Callable和Future面试知识点总结,看完我直接面进了字节跳动,原谅我有点飘了(下)
- (JS) responsibility chain mode
- Micro blog comment architecture design
- (JS) observer mode
- (JS) imitate an instanceof method
猜你喜欢
![[NLP] text generation topic 1: basic knowledge](/img/cf/2e29e55344ea4cc8ce48777878b867.png)
[NLP] text generation topic 1: basic knowledge

Multithreaded high concurrency server: three problems

Qt学习05 Qt Creator工程介绍

微博评论架构设计

巴比特 | 元宇宙每日必读:HTC 宣布推出首款元宇宙手机,售价约2700元人民币,都有哪些新玩法?...

斐波那锲数列与冒泡排序法在C语言中的用法

TTL serial port learning infrared remote control module can be extended to network control

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

How to identify the exact length and width of the contour

关于IP定位查询接口的测评Ⅱ
随机推荐
CTO专访:合见工软深化产品布局 加速国产EDA技术革新
Online sql to htmltable tool
QT learning 11 string classes in QT
QT learning 15 separation of user interface and business logic
Shell 引号和转义从来很少被人注意,但平时写脚本又经常用
Qt学习01 GUI程序原理分析
misc3~7
【HBZ分享】Semaphore 与 CountDownLatch原理
Spark - one to one correspondence between task and partition and detailed explanation of parameters
Multi thread communication between client and server (primary version)
9 easy to use JSON processing tools, greatly improving efficiency!
Xuetong denies that the theft of QQ number is related to it: it has been reported; IPhone 14 is ready for mass production: four models are launched simultaneously; Simple and elegant software has long
极限导论总结
Encore une fois, le chemin de l'amélioration de redis Cloud
Bs-gx-018 student examination system based on SSM
彻头彻尾理解JVM系列之七:对象在分代模型中的流转过程是怎样的?
Cornerstone of efficient remote office: effective communication | community essay solicitation
Several types of MySQL index invalidation and their solutions
[digital signal modulation] realize signal modulation and demodulation based on am+fm+dsb+ssb, including Matlab source code
Limit introduction summary