Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
3
Star
47
Fork
22
DreamCoders
/
CoderGuide
Code
Issues
1169
Pull Requests
0
Wiki
Insights
Pipelines
Service
JavaDoc
PHPDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
DevLens
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
说一下 HashSet 的实现原理?
Backlog
#IAJKQ0
陌生人
owner
Opened this issue
2024-08-13 10:04
<p><span style="color: rgb(51, 51, 51);">HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">是基于</span><span style="color: rgb(51, 51, 51);"> HashMap </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">实现的,</span><span style="color: rgb(51, 51, 51);">HashSet</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的值存放于</span><span style="color: rgb(51, 51, 51);">HashMap</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的</span><span style="color: rgb(51, 51, 51);">key</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">上,</span><span style="color: rgb(51, 51, 51);">HashMap</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的</span><span style="color: rgb(51, 51, 51);">value</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">统一为 </span></p><p><span style="color: rgb(51, 51, 51);">PRESENT</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">,因此</span><span style="color: rgb(51, 51, 51);"> HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的实现比较简单,相关</span><span style="color: rgb(51, 51, 51);"> HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的操作,基本上都是直接调用底层 </span></p><p><span style="color: rgb(51, 51, 51);">HashMap </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的相关方法来完成,</span><span style="color: rgb(51, 51, 51);">HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">不允许重复的值。</span></p><p style="text-align: left;">HashSet底层使用了哈希表来支持的,特点:存储快 往HashSet添加元素的时候,HashSet会先调用元素的HashCode方法得到元素的哈希值,然后通过元素的哈希值经过异或移位等运算,就可以算出该元素在哈希表中的存储位置。</p><h2 style="text-align: left; line-height: 1.5;">运行原理</h2><p style="text-align: left;">如果算出的元素存储的位置目前没有任何元素存储,name该元素可以直接存储在该位置上;如果算出的元素的存储位置上目前已经有了其他的元素,那么还会调用该元素的 equals方法 ,与该位置的元素进行比较一次,如果过equals方法返回的是true,那么该位置上的元素就会被视为重复元素,不允许被添加,如果false,则允许添加。</p><h2 style="text-align: left; line-height: 1.5;">实现原理</h2><p style="text-align: left;">hashset是基于hashmap实现的,默认构造函数是构建一个初始容量为16,负载因子为0.75的hashmap。封装了一个hashmap 对象来存储所有的集合元素,所有放在 hashset中的集合元素实际上由 hashmap的key来保存,而 hashset中的 hashmap的 value则存储了一个PRESENT的静态object对象</p><h2 style="text-align: left; line-height: 1.5;">hashset和 treeset有什么区别</h2><p style="text-align: left;">hashset是由一个hash表来实现的,因此它的元素是无序的,add,remove,contains方法的时间复杂度是 O(1)</p><p style="text-align: left;">treeset是由一个树形结构来实现的,它里面的元素是有序的,因此,add,remove,contains方法的时间复杂度是 O(logn)</p>
<p><span style="color: rgb(51, 51, 51);">HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">是基于</span><span style="color: rgb(51, 51, 51);"> HashMap </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">实现的,</span><span style="color: rgb(51, 51, 51);">HashSet</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的值存放于</span><span style="color: rgb(51, 51, 51);">HashMap</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的</span><span style="color: rgb(51, 51, 51);">key</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">上,</span><span style="color: rgb(51, 51, 51);">HashMap</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的</span><span style="color: rgb(51, 51, 51);">value</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">统一为 </span></p><p><span style="color: rgb(51, 51, 51);">PRESENT</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">,因此</span><span style="color: rgb(51, 51, 51);"> HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的实现比较简单,相关</span><span style="color: rgb(51, 51, 51);"> HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的操作,基本上都是直接调用底层 </span></p><p><span style="color: rgb(51, 51, 51);">HashMap </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的相关方法来完成,</span><span style="color: rgb(51, 51, 51);">HashSet </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">不允许重复的值。</span></p><p style="text-align: left;">HashSet底层使用了哈希表来支持的,特点:存储快 往HashSet添加元素的时候,HashSet会先调用元素的HashCode方法得到元素的哈希值,然后通过元素的哈希值经过异或移位等运算,就可以算出该元素在哈希表中的存储位置。</p><h2 style="text-align: left; line-height: 1.5;">运行原理</h2><p style="text-align: left;">如果算出的元素存储的位置目前没有任何元素存储,name该元素可以直接存储在该位置上;如果算出的元素的存储位置上目前已经有了其他的元素,那么还会调用该元素的 equals方法 ,与该位置的元素进行比较一次,如果过equals方法返回的是true,那么该位置上的元素就会被视为重复元素,不允许被添加,如果false,则允许添加。</p><h2 style="text-align: left; line-height: 1.5;">实现原理</h2><p style="text-align: left;">hashset是基于hashmap实现的,默认构造函数是构建一个初始容量为16,负载因子为0.75的hashmap。封装了一个hashmap 对象来存储所有的集合元素,所有放在 hashset中的集合元素实际上由 hashmap的key来保存,而 hashset中的 hashmap的 value则存储了一个PRESENT的静态object对象</p><h2 style="text-align: left; line-height: 1.5;">hashset和 treeset有什么区别</h2><p style="text-align: left;">hashset是由一个hash表来实现的,因此它的元素是无序的,add,remove,contains方法的时间复杂度是 O(1)</p><p style="text-align: left;">treeset是由一个树形结构来实现的,它里面的元素是有序的,因此,add,remove,contains方法的时间复杂度是 O(logn)</p>
Comments (
0
)
Sign in
to comment
Status
Backlog
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
Java
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
Branches (
-
)
Tags (
-
)
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register