当前位置:网站首页>HashSet源码解析
HashSet源码解析
2022-07-30 22:48:00 【骆驼整理说】
目录
HashSet
HashSet都是基于HashMap来实现的。HashSet的特点如下:
- 内部使用HashMap的key存储元素,以此来保证元素不重复;
- HashSet 是无序的,因为 HashMap 的 key 是无序的;
- HashSet 中允许有一个 null 元素,因为 HashMap 允许 key 为 null;
- HashSet 是非线程安全的。
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable {
static final long serialVersionUID = -5024744406713321676L;
// 基于HashMap实现
private transient HashMap<E,Object> map;
// 只需要用到HashMap中key唯一的特性,所以value全部使用同一个 Object实例填充,节省内存空间
private static final Object PRESENT = new Object();
/**
* 实例化 HashSet 的时候,初始化内部的 HashMap
*/
public HashSet() {
map = new HashMap<>();
}
/**
* 根据一个集合实例,实例化 HashSet
*/
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
/**
* 根据初始容量和扩容因子实例化 HashSet,减少rehash频率,提升性能,原理与HashMap相同
*/
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);
}
/**
* 返回迭代器,用于迭代
* 下面所有的功能都是基于 HashMap 来实现的
*/
public Iterator<E> iterator() {
return map.keySet().iterator();
}
/**
* 元素个数
*/
public int size() {
return map.size();
}
/**
* 是否为空
*/
public boolean isEmpty() {
return map.isEmpty();
}
/**
* 是否包含给定元素
*/
public boolean contains(Object o) {
return map.containsKey(o);
}
/**
* 添加元素,如果 Set集合中未包含该元素,返回true
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
/**
* 删除元素,如果Set集合包含该元素,返回true
*/
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
/**
* 清除元素
*/
public void clear() {
map.clear();
}
/**
* 浅克隆
*/
@SuppressWarnings("unchecked")
public Object clone() {
try {
HashSet<E> newSet = (HashSet<E>) super.clone();
newSet.map = (HashMap<E, Object>) map.clone();
return newSet;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
}TreeSet
边栏推荐
- for...in 和 for...of 的区别
- “蔚来杯“2022牛客暑期多校训练营4 L.Black Hole 垃圾计算几何
- 第十九周进度(了解物联网基础知识)
- Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
- A detailed explanation: SRv6 Policy model, calculation and drainage
- Gxlcms有声小说系统/小说听书系统源码
- Day016 Classes and Objects
- ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
- OpenCV笔记(二十):滤波函数——filter2D
- MySQL联合查询(多表查询)
猜你喜欢

2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】

Alibaba Cloud video on demand + project combat

一文详解:SRv6 Policy模型、算路及引流
![[MySQL] Related operations on databases and tables in MySQL](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[MySQL] Related operations on databases and tables in MySQL

Gxlcms有声小说系统/小说听书系统源码

【无标题】

MySQL 8.0.29 set and modify the default password

2sk2225代换3A/1500V中文资料【PDF数据手册】

EasyExcel综合课程实战

Week 19 Progress (Understanding IoT Basics)
随机推荐
Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
Chapter 8 Intermediate Shell Tools II
# # yyds dry goods inventory interview will brush TOP101: to determine whether there is a part of the list
【无标题】
Successfully resolved ModuleNotFoundError: No module named 'Image'
OpenCV笔记(二十):滤波函数——filter2D
win10重建索引
阿里云视频点播+项目实战
只会纯硬件,让我有点慌
Ningbo Zhongning Pawn will transfer 29.5% of the equity for 2.8338 million yuan, and the owner's equity in 2021 will be 9.6875 million yuan
tcp协议传输中的粘包问题
mysql跨库关联查询(dblink)
【无标题】
Apache Doris series: detailed steps for installation and deployment
Rust编译报错:error: linker `cc` not found
@RequestBody、 @RequestParam 、 @PathVariable 和 @Vaild 注解
2022.7.28
IDEA使用技巧
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
#yyds干货盘点# 面试必刷TOP101:判断链表中是否有环