Does adding a duplicate value to a HashSet/HashMap replace the previous value(向 HashSet/HashMap 添加重复值是否会替换先前的值)
问题描述
请考虑以下代码:
HashSet hs = new HashSet();
hs.add("hi"); -- (1)
hs.add("hi"); -- (2)
hs.size() 将给出 1,因为 HashSet 不允许重复,因此只会存储一个元素.
hs.size() will give 1 as HashSet doesn't allow duplicates so only one element will be stored.
我想知道如果我们添加了重复元素,那么它是替换前一个元素还是根本不添加它?
I want to know if we add the duplicate element, then does it replace the previous element or it simply doesn't add it?
另外,对于同样的情况,使用HashMap会发生什么?
Also, what will happen usingHashMap for the same case?
推荐答案
以HashMap,它将旧值替换为新值.
In the case of HashMap, it replaces the old value with the new one.
在 的情况下HashSet,该项没有插入.
In the case of HashSet, the item isn't inserted.
这篇关于向 HashSet/HashMap 添加重复值是否会替换先前的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向 HashSet/HashMap 添加重复值是否会替换先前的值
基础教程推荐
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
