登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
3
Star
47
Fork
23
DreamCoders
/
CoderGuide
代码
Issues
1169
Pull Requests
0
Wiki
统计
流水线
服务
JavaDoc
PHPDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
开发画像分析
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
HashSet如何检查重复?HashSet是如何保证数据不可重复的?
待办的
#IAJKPX
陌生人
拥有者
创建于
2024-08-13 10:04
<p><br></p><p><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);">add ()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">元素时,判断元素是否存在的依据,不仅要比较</span><span style="color: rgb(51, 51, 51);">hash</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值,同时还要结合</span><span style="color: rgb(51, 51, 51);">equles </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">比较。 </span></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);">add ()</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);">put()</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);"> key </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></p><p><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);">K/V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">相同时,会用新的</span><span style="color: rgb(51, 51, 51);">V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">覆盖掉旧的</span><span style="color: rgb(51, 51, 51);">V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">,然后返回旧的</span><span style="color: rgb(51, 51, 51);">V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">。所以不会重复(</span><span style="color: rgb(51, 51, 51);"> HashMap </span></p><p><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);"> hashcode </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">再比较</span><span style="color: rgb(51, 51, 51);">equals </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">)。 </span></p><p><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><pre><code class="language-java">private static final Object PRESENT = new Object(); private transient HashMap<E,Object> map; public HashSet() { <> map = new HashMap (); } public boolean add(E e) { // 调用HashMap的put方法,PRESENT是一个至始至终都相同的虚值 return map.put(e, PRESENT)==null; }</code></pre><p><span style="color: rgb(51, 51, 51);">hashCode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">()与</span><span style="color: rgb(51, 51, 51);">equals</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">()的相关规定: </span></p><p><span style="color: rgb(51, 51, 51);">1. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">如果两个对象相等,则</span><span style="color: rgb(51, 51, 51);">hashcode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">一定也是相同的 </span></p><p><span style="color: rgb(51, 51, 51);">2. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">两个对象相等</span><span style="color: rgb(51, 51, 51);">,</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">对两个</span><span style="color: rgb(51, 51, 51);">equals</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法返回</span><span style="color: rgb(51, 51, 51);">true </span></p><p><span style="color: rgb(51, 51, 51);">3. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">两个对象有相同的</span><span style="color: rgb(51, 51, 51);">hashcode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值,它们也不一定是相等的 </span></p><p><span style="color: rgb(51, 51, 51);">4. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">综上,</span><span style="color: rgb(51, 51, 51);">equals</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法被覆盖过,则</span><span style="color: rgb(51, 51, 51);">hashCode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法也必须被覆盖 </span></p><p><span style="color: rgb(51, 51, 51);">5. hashCode()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的默认行为是对堆上的对象产生独特值。如果没有重写</span><span style="color: rgb(51, 51, 51);">hashCode()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">,则该</span><span style="color: rgb(51, 51, 51);">class</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的两个 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">对象无论如何都不会相等(即使这两个对象指向相同的数据)。</span></p>
<p><br></p><p><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);">add ()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">元素时,判断元素是否存在的依据,不仅要比较</span><span style="color: rgb(51, 51, 51);">hash</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值,同时还要结合</span><span style="color: rgb(51, 51, 51);">equles </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">比较。 </span></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);">add ()</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);">put()</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);"> key </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></p><p><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);">K/V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">相同时,会用新的</span><span style="color: rgb(51, 51, 51);">V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">覆盖掉旧的</span><span style="color: rgb(51, 51, 51);">V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">,然后返回旧的</span><span style="color: rgb(51, 51, 51);">V</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">。所以不会重复(</span><span style="color: rgb(51, 51, 51);"> HashMap </span></p><p><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);"> hashcode </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">再比较</span><span style="color: rgb(51, 51, 51);">equals </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">)。 </span></p><p><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><pre><code class="language-java">private static final Object PRESENT = new Object(); private transient HashMap<E,Object> map; public HashSet() { <> map = new HashMap (); } public boolean add(E e) { // 调用HashMap的put方法,PRESENT是一个至始至终都相同的虚值 return map.put(e, PRESENT)==null; }</code></pre><p><span style="color: rgb(51, 51, 51);">hashCode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">()与</span><span style="color: rgb(51, 51, 51);">equals</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">()的相关规定: </span></p><p><span style="color: rgb(51, 51, 51);">1. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">如果两个对象相等,则</span><span style="color: rgb(51, 51, 51);">hashcode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">一定也是相同的 </span></p><p><span style="color: rgb(51, 51, 51);">2. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">两个对象相等</span><span style="color: rgb(51, 51, 51);">,</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">对两个</span><span style="color: rgb(51, 51, 51);">equals</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法返回</span><span style="color: rgb(51, 51, 51);">true </span></p><p><span style="color: rgb(51, 51, 51);">3. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">两个对象有相同的</span><span style="color: rgb(51, 51, 51);">hashcode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">值,它们也不一定是相等的 </span></p><p><span style="color: rgb(51, 51, 51);">4. </span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">综上,</span><span style="color: rgb(51, 51, 51);">equals</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法被覆盖过,则</span><span style="color: rgb(51, 51, 51);">hashCode</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">方法也必须被覆盖 </span></p><p><span style="color: rgb(51, 51, 51);">5. hashCode()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的默认行为是对堆上的对象产生独特值。如果没有重写</span><span style="color: rgb(51, 51, 51);">hashCode()</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">,则该</span><span style="color: rgb(51, 51, 51);">class</span><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">的两个 </span></p><p><span style="color: rgb(51, 51, 51); font-family: 微软雅黑;">对象无论如何都不会相等(即使这两个对象指向相同的数据)。</span></p>
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
Java
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (
-
)
标签 (
-
)
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册