当前位置:网站首页>HashSet内部原理解析
HashSet内部原理解析
2022-07-28 09:01:00 【俞其荣】
注:本文解析的 HashSet 源代码基于 Java 1.8 。
Header
HashSet是用来存储没有重复元素的集合类,并且它是无序的。
HashSet 内部实现是基于 HashMap ,实现了 Set 接口。
源码解析
构造方法
public HashSet() {
map = new HashMap<>();
}
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}我们发现,除了最后一个 HashSet 的构造方法外,其他所有内部就是去创建一个 Hashap 。没有其他的操作。而最后一个构造方法不是 public 的,所以不对外公开。
add
public boolean add(E e) {
// PRESENT = new Object()
return map.put(e, PRESENT)==null;
}add 方法很简单,就是在 map 中放入一键值对。 key 就是要存入的元素,value 是 PRESENT ,其实就是 new Object() 。所以,HashSet 是不能重复的。
remove
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}相应的,remove 就是从 map 中移除 key 。
contains
public boolean contains(Object o) {
return map.containsKey(o);
}这些代码应该很明白,不需要讲了。
iterator
public Iterator<E> iterator() {
return map.keySet().iterator();
}内部调用的就是 HashMap 中 keySet 的 iterator 方法。
size
public int size() {
return map.size();
}剩下的 HashSet 方法也不多,内部也都是通过 HashMap 实现的。就不贴出来了,大家回去看一下都会明白的。
Footer
从上看下来,HashSet 的源码是挺简单的,内部都是用 HashMap 来实现的。利用了 HashMap 的 key 不能重复这个原理来实现 HashSet 。
内容很简短,都讲完了,再见。
边栏推荐
- Get started quickly with flask (I) understand the framework flask, project structure and development environment
- Problems encountered in upgrading golang to version 1.18.4
- mq的学习
- [592. Fraction addition and subtraction]
- 51 single chip microcomputer storage: EEPROM (I2C)
- How does gbase 8A use preprocessing to quickly insert data?
- 【AUTOSAR-RTE】-3-Runnable及其Task Mapping映射
- 2022安全员-C证特种作业证考试题库及答案
- [English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared
- Magic brace- [group theory] [Burnside lemma] [matrix fast power]
猜你喜欢
![[package deployment]](/img/6f/93a35436947311bc2305adcb0df1a6.png)
[package deployment]

2022年起重机司机(限桥式起重机)考试题库及模拟考试
![[附下载]推荐几款暴力破解和字典生成的工具](/img/c6/f4a9c566ff21a8e133a8a991108201.png)
[附下载]推荐几款暴力破解和字典生成的工具

什么是跨域?如何解决请跨域问题?
![[high number] high number plane solid geometry](/img/fc/da6aefed48f4adbaf58995b5e8fa46.png)
[high number] high number plane solid geometry

OpenShift 4 - 使用 VerticalPodAutoscaler 优化应用资源 Request 和 Limit

21 day learning challenge - "AUTOSAR from introduction to mastery - practical part"

MQTT.js 入门教程:学习笔记

【打包部署】

对话MySQL之父:代码一次性完成才是优秀程序员
随机推荐
网络工程——软科中国大学专业排名
【AUTOSAR-RTE】-3-Runnable及其Task Mapping映射
final关键字和枚举类型
Informatics Olympiad all in one 1617: circle game | 1875: [13noip improvement group] circle game | Luogu p1965 [noip2013 improvement group] circle game
21 day learning challenge - "AUTOSAR from introduction to mastery - practical part"
sql server 的关键字在哪张系统表?
[one flower, one world - Professor Zheng Yi - the way of simplicity] interpretable neural network
对话MySQL之父:代码一次性完成才是优秀程序员
2022高压电工考试模拟100题及模拟考试
The new mode of 3D panoramic display has become the key to breaking the game
Common prototype methods of canvas and the application of drawing pictures
DN-DETR 论文精度,并解析其模型结构 & 2022年CVPR论文
正负数值的正则表达式
【多线程】println方法底层原理
MQTT. JS introductory tutorial: learning notes
376. 摆动序列【贪心、动态规划------】
Pytorch deep learning practice lesson 9 multi classification problems (handwritten numeral MNIST)
[C language] detailed explanation sequence table (seqlist)
负数的十六进制表示
7 C control statements: branches and jumps