当前位置:网站首页>Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
2022-07-03 08:27:00 【bai259257】
package Set;
import java.util.Collections;
import java.util.HashSet;
public class HashSetDemo01 {
public static void main(String[] args) {
//HashSet The underlying data structure of a collection is a hash table , It is a secondary data structure , The hash table is composed of an array + Linked list implementation of
//Set The characteristic of a set is that the elements are unique , Because in its system, although HashSet The order in which the set accesses data is out of order ( Of course, it may also be orderly ),
// But through in-depth analysis , We can actually see , The order of our cognition refers to the order of access ,HashSet The set is based on hash value pairs 16 Take the rest to decide which bucket to store , There is also a sequence of going deep into the bottom
//HashSet The collection has 0~15 An array of indexes of .
HashSet<dog> hashSet = new HashSet<>();
Collections.addAll(hashSet,new dog(" Darling ",1),new dog(" Recruit money ",3),new dog(" Recruit money ",3));
// We store three elements in the collection , Suppose that the two incoming fortunes are the same dog , But by printing the set, we can see that both dogs have been added
System.out.println(hashSet);

// reflection :HashSet The set should be unique to the element , However, elements with the same content can be added to the collection , Why does this happen here ?
// We need to know a method first public int hashCode(), This method is Object Class , Any object calls hashCode Methods will return the corresponding hash value , The default is generated according to the address value
// What we need to know , because int The data is -2147483648~2147483647, If there are enough elements , Object call hashCode The data obtained by the method will certainly be repeated , This is called hash collision .
//HashSet The underlying data structure of a collection is an array + Linked list , When creating this collection , It initializes a with a length of 16 Array of , Each array stores linked list nodes . Its storage characteristics are determined by the hash value , It will make every element call hashCode Method , Make hash value pairs 16 Remainder , The remainder is stored in the bucket , Judge whether there are elements in the bucket , If there is no element , It is stored as the head node of the current bucket
// If there are elements in the bucket ,(1) First judge and compare the hash value of this element with the hash value of the bucket , Even if the hash values of two elements are the same , The system will not think that elements are repeated , Because hash collision may occur , Although the possibility is relatively small , But it can also happen
//(2) Next we will use equals Method to compare the address value of the element to be stored with the address value of the element in the bucket , Because the objects are new Coming out , The address in the heap space is unique , So whether the content is the same or not , Will be stored in the collection
// Here we can make it clear ,hashSet Collections store data elements , Is determined by the hash value , The underlying hash value is determined by the address value . Originally in hashSet Determine whether the elements are repeated in the set In the final analysis, it is to judge the address value of two elements !
// And we don't want to add elements through address values at all , Instead, I want to judge the content .
// Solutions :
// Override in the class of the object element equals as well as hashCode Method .
// The rewritten equals The bottom layer of the method is to compare the content ,hashCode Methods are also generated based on content .
// After rewriting , Set stored data , It still makes every element call hashCode Method to get the hash value , We need to know that the current hash value is generated through content , So if there are elements in the bucket to be stored , Judge whether the hash value of the element in the bucket is the same , If it's different, deposit it directly ,
// If the same, it will be considered that hash collision occurs ,(2) Use equals Method to judge the content between elements , If different, it is stored in the set , If true It is clear that this is a repeating element !

}
}
边栏推荐
- matlab神经网络所有传递函数(激活函数)公式详解
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
- [usaco12mar]cows in a skyscraper g (state compression DP)
- MAE
- php-fpm软件的安装+openresty高速缓存搭建
- ArrayList
- Student educational administration management system of C # curriculum design
- 【音视频】ijkplayer错误码
- Pit & ADB wireless debugging of vivo real machine debugging
猜你喜欢

Three characteristics

Unity learning notes

图像处理8-CNN图像分类

Haproxy+kept cluster setup 02

animation

数据库应用技术课程设计之商城管理系统

Shader foundation 01

Transplantation of freetype Library

About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
![[updating] wechat applet learning notes_ three](/img/05/958b8d62d3a42b38ca1a2d8631a7f8.png)
[updating] wechat applet learning notes_ three
随机推荐
Find the intersection of line segments
P1596 [USACO10OCT]Lake Counting S
About Wireshark's unsuccessful installation of npcap
Golang time format sorting
Golang中删除字符串的最后一个字符
Advanced OSG collision detection
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Osgconv tool usage
C language - Introduction - essence Edition - take you into programming (I)
Golang 中string和int类型相互转换
使用base64编码传图片
十六进制编码简介
Notes on understanding applets 2022/7/3
swagger文档配置
Cesium service deployment, and import and display local 3dfiles data
Unity4.3.1 engine source code compilation process
Image processing 8-cnn image classification
Unity editor expansion - the design idea of imgui
matlab神经网络所有传递函数(激活函数)公式详解
MySQL 8