当前位置:网站首页>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 。
内容很简短,都讲完了,再见。
边栏推荐
- Alibaba cloud server setup and pagoda panel connection
- 脉冲风采|Committer 专访——腾讯工程师张大伟喊你吃“螃蟹”啦
- [solution] error in [eslint] eslint is not a constructor
- F - jealous two-dimensional reverse order pair
- 19c SYSAUX表空间SQLOBJ$PLAN表过大,如何清理
- C# 窗体应用使用对象绑定 DataGridView 数据绑定
- final关键字和枚举类型
- 技术分享| 快对讲综合调度系统
- Oracle-11gr2 default system job
- ARouter源码解析(三)
猜你喜欢

Technology sharing | quick intercom integrated dispatching system

Openshift 4 - use verticalpodautoscaler to optimize application resource request and limit

Promise学习笔记

golang升级到1.18.4版本 遇到的问题

2022 high voltage electrician examination simulated 100 questions and simulated examination

opencv安装配置测试

2022高压电工考试模拟100题及模拟考试

Promise learning notes

OpenShift 4 - 使用 VerticalPodAutoscaler 优化应用资源 Request 和 Limit
![376. Swing sequence [greedy, dynamic planning -----]](/img/c3/46cdd8c9320c529171cbf963c768a7.png)
376. Swing sequence [greedy, dynamic planning -----]
随机推荐
Promise学习笔记
QT basic hand training applet - simple calculator design (with source code, analysis)
译文推荐 | 调试 BookKeeper 协议 - 无界 Ledger
2022 safety officer-c certificate special operation certificate examination question bank and answers
VR panoramic shooting helps promote the diversity of B & B
《我的Vivado实战—单周期CPU指令分析》
[solution] error in [eslint] eslint is not a constructor
[Download] several tools for brute force cracking and dictionary generation are recommended
ARouter源码解析(二)
js数组去重,id相同对某值相加合并
Face warp - hand tear code
How promise instance solves hell callback
技术分享| 快对讲综合调度系统
2.9.5 Ext JS的Object类型处理及便捷方法
Promise learning notes
2022年安全员-B证考试模拟100题及答案
[592. Fraction addition and subtraction]
F - jealous two-dimensional reverse order pair
An entry artifact tensorflowplayground
Express builds a simple local background (1)