当前位置:网站首页>Equals increases execution speed / performance optimization
Equals increases execution speed / performance optimization
2022-06-29 11:42:00 【Hard work boy】
In principle, it is close to bron filter .
When you add objects to HashSet when ,HashSet Meeting First calculate the value of the object hashcode value To determine where objects are added , At the same time With other objects that have been added to this location hashcode Comparison of values , If No match Of hashcode,HashSet Meeting Assume that the object does not recur . But if you find It's the same hashcode value The object of , And then it will call equals() Methods to check hashcode Are equal objects really the same . If The two are the same ,HashSet It won't make the join operation successful . If it's different , It will be re hashed to other locations .
public class test1 {
public static void main(String[] args) {
String a = new String("ab"); // a For a quote
String b = new String("ab"); // b For another reference , Object has the same content
String aa = "ab"; // Put in constant pool
String bb = "ab"; // Find from constant pool
if (aa == bb) // true
System.out.println("aa==bb");
if (a == b) // false, Not the same object
System.out.println("a==b");
if (a.equals(b)) // true
System.out.println("aEQb");
if (42 == 42.0) // true
System.out.println("true");
}
}
This greatly reduces equals The number of times , Accordingly, the execution speed is greatly improved . We can see from it that : hashCode() Its function is to get hash code , Also known as hash code ; It actually returns a int Integers . The function of this hash code is to determine the index position of the object in the hash table .hashCode() Only useful in hash tables , In other cases it doesn't work . In the hash table hashCode() Is used to get the hash code of an object , Then determine the position of the object in the hash table .
边栏推荐
猜你喜欢

What is the experience of working in an IT company in Japan?

Cornerstone of efficient remote office: effective communication | community essay solicitation

Pipeline aggregations管道聚合- parent-2

Rebuild confidence in China's scientific research - the latest nature index 2022 released that China's research output increased the most
![[daily 3 questions (1)] judge the color of a grid on the chess board](/img/bd/064058f917554237b741795d405189.png)
[daily 3 questions (1)] judge the color of a grid on the chess board

Qt学习01 GUI程序原理分析

CTO专访:合见工软深化产品布局 加速国产EDA技术革新

【文献翻译】Concealed Object Detection(伪装目标检测)

Micro blog comment architecture design

Bs-gx-018 student examination system based on SSM
随机推荐
稳定币风险状况:USDT 和 USDC 安全吗?
海泰前沿技术|隐私计算技术在医疗数据保护中的应用
Encore une fois, le chemin de l'amélioration de redis Cloud
(JS) handwriting depth comparison
斐波那锲数列与冒泡排序法在C语言中的用法
Self-Improvement! Junior college "counter attack" master of Zhejiang University, 3 SCI, and finally become a doctor of Tsinghua University!
Leetcode 535 encryption and decryption of tinyurl [map] the leetcode road of heroding
QT learning 09 calculator interface code reconstruction
ESP8266简介:三种编程方式「建议收藏」
安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会
【每日3题(1)】判断国际象棋棋盘中一个格子的颜色
信息技术应用创新专业人员(数据库)中级培训火热招生中(7月6-10日)
Several types of MySQL index invalidation and their solutions
Safety innovation practice | Haitai Fangyuan was invited to participate in the technical exchange Seminar on "network information innovation and value co creation in the digital age"
Necessary for cloud native development: the first common codeless development platform IVX editor
MySQL开启慢查询
MMdet中的Resnet源码解读+Ghost模块
(JS) isnan() method determines the reason why undefined is true
Nature | 全球海洋微生物组的生物合成潜力
Add notification announcements to send notifications to online users