当前位置:网站首页>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 。
内容很简短,都讲完了,再见。
边栏推荐
- 2022年安全员-B证考试模拟100题及答案
- Promise学习笔记
- The new mode of 3D panoramic display has become the key to breaking the game
- Final keyword and enumeration type
- 译文推荐 | 调试 BookKeeper 协议 - 无界 Ledger
- [autosar-rte] - 3-runnable and its task mapping mapping
- RGB-T追踪——【多模态融合】Visible-Thermal UAV Tracking: A Large-Scale Benchmark and New Baseline
- VR panoramic shooting helps promote the diversity of B & B
- 21 day learning challenge - "AUTOSAR from introduction to mastery - practical part"
- [package deployment]
猜你喜欢

2022年危险化学品经营单位安全管理人员上岗证题目及答案

Network engineering -- ranking of Chinese universities in Soft Science

C simply call FMU for simulation calculation

Get started quickly with flask (I) understand the framework flask, project structure and development environment

2022年安全员-B证考试模拟100题及答案
![[592. Fraction addition and subtraction]](/img/3a/1a76a8acd60a6d45eebed612fd3971.png)
[592. Fraction addition and subtraction]

Conditions and procedures of stock index futures account opening

什么是跨域?如何解决请跨域问题?

What is cross domain? How to solve the cross domain problem?
![[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared](/img/a8/2c2fab613035f5e50524056d5f51a3.png)
[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared
随机推荐
MATLAB的实时编辑器
How promise instance solves hell callback
2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
Oracle creates users with query permission only
Network engineering -- ranking of Chinese universities in Soft Science
如何在多线程环境下使用 GBase C API ?
Magic Bracelet-【群论】【Burnside引理】【矩阵快速幂】
面经-手撕代码
opencv安装配置测试
Regular expressions for positive and negative values
数据库核心体系
5 operators, expressions, and statements
2022年危险化学品经营单位安全管理人员上岗证题目及答案
数据库那么多概念性的东西怎么学?求方法
01 tensorflow calculation model (I) - calculation diagram
CakePHP 4.4.3 发布,PHP 快速开发框架
MySQL 8.0.30 GA
[vscode] vscode usage
Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline
2022 high voltage electrician examination simulated 100 questions and simulated examination