JDK 8 - java.util.HashSet 实现机制分析
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, Serializable
1、实现了java.util.Set接口,内部由java.util.Map示例实现(允许null值)
2、不保证迭代顺序,也不保证迭代顺序一直不变
3、非同步;必须从外部同步,比如:Set s = Collections.synchronizedSet(new HashSet());
4、iterator()返回的iterator是fail-fast的:如果这个iterator创建以后,这个set被修改了(不是这个iterator的remove()方法修改的),那么会抛出ConcurrentModificationException异常。(注意,这个fail-fast也是无法保证的,因为在非同步的并发修改中,没有什么是可以保证的)
UML Class Diagram:

HashSet 内部有一个变量:
private transient HashMap<E,Object> map;
HashSet 的所有操作最后都是委托给了这个 hashmap。
1、添加元素
1 public boolean add(E e) {
2 // 到 map 中查找,key==e && value==PRESENT
3 return map.put(e, PRESENT)==null;
4 }
2、删除元素
1 public boolean remove(Object o) {
2 // 从 map 中删除 key==o 的元素,其中 key==o 对应的 value==PRESENT
3 return map.remove(o)==PRESENT;
4 }
3、查找元素
public boolean contains(Object o) {
// 到 map 中查找,key==o
return map.containsKey(o);
}
常见操作:
| Operations | Time Complexity | Notes |
| add, remove, contains, size | O(1) | assuming the hash functions has dispersed the elements properly among the buckets |
| iteration | proportional to the number of the elememts and buckets | do not set the initial capacity too high(or the load factor too low) for the iteration-critical program |
- Implementing the java.util.Set interface, internally backed by a java.util.HashMap instance(permitting null values).
- Making no guarantees to the iteration order and the constantness of the order over time either.
- Not synchronized; must be synchronized externally, for example: Set s = Collections.synchronizedSet(new HashSet());
- Iterators returned by the iterator() method are fail-fast: it will throw a ConcurrentModificationException if the set is modified after the creation of the iterator and by any method except through the iterator's remove() method. Notice that the fail-fast behavior cannot be guaranteed because nothing can be guaranteed in the presence of unsynchronized concurrent modification.
JDK 8 - java.util.HashSet 实现机制分析的更多相关文章
- JDK 8 - java.util.HashMap 实现机制分析
官方文档对 HashMap 的定义: public class HashMap<K,V> extends AbstractMap<K,V> implements Map< ...
- java.util.HashSet源码分析
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java. ...
- java.util.HashSet, java.util.LinkedHashMap, java.util.IdentityHashMap 源码阅读 (JDK 1.8)
一.java.util.HashSet 1.1 HashSet集成结构 1.2 java.util.HashSet属性 private transient HashMap<E,Object> ...
- java.util.HashSet, java.util.LinkedHashMap, java.util.IdentityHashMap 源码阅读 (JDK 1.8.0_111)
一.java.util.HashSet 1.1 HashSet集成结构 1.2 java.util.HashSet属性 private transient HashMap<E,Object> ...
- 原子类java.util.concurrent.atomic.*原理分析
原子类java.util.concurrent.atomic.*原理分析 在并发编程下,原子操作类的应用可以说是无处不在的.为解决线程安全的读写提供了很大的便利. 原子类保证原子的两个关键的点就是:可 ...
- java.util.concurrent各组件分析 一 sun.misc.Unsafe
java.util.concurrent各组件分析 一 sun.misc.Unsafe 说到concurrent包也叫并发包,该包下主要是线程操作,方便的进行并发编程,提到并发那么锁自然是不可缺少的, ...
- java.util.concurrent包详细分析--转
原文地址:http://blog.csdn.net/windsunmoon/article/details/36903901 概述 Java.util.concurrent 包含许多线程安全.测试良好 ...
- java.util.Collection源码分析和深度讲解
写在开头 java.util.Collection 作为Java开发最常用的接口之一,我们经常使用,今天我带大家一起研究一下Collection接口,希望对大家以后的编程以及系统设计能有所帮助,本文所 ...
- java.util.Hashtable源码分析
Hashtable实现一个键值映射的表.任何非null的object可以用作key和value. 为了能存取对象,放在表里的对象必须实现hashCode和equals方法. 一个Hashtable有两 ...
随机推荐
- Qt构造函数的参数:QObject *parent = Q_NULLPTR
几乎所有的Qt类的构造函数都会有一个parent参数.这个参数通常是QObject* 或者是 QWidget* 类型的.很多情况下它都会有一个初始值0,因此,即便你不去给它复制也没有丝毫的问题.于是, ...
- INSPIRED启示录 读书笔记 - 第19章 用户体验设计与实现
先定义用户体验再动手开发 在软件开发过程中,有很多工作可以同时进行.比如,需求调研和产品设计(用户体验设计).开发与测试 尽管如此,用户体验设计和软件开发就不能同时进行,原因有五点 1.与软件开发团队 ...
- Java远程执行Shell命令
1. Jar包:ganymed-ssh2-build210.jar 2. 步骤: a) 连接: Connection conn = new Connection(ipAddr); conn.conne ...
- JDK的安装配置
1.下载JDK安装包(http://www.oracle.com/technetwork/java/javase/downloads/index.html),现在Java已经更新到JDK 8了,但是很 ...
- linux crontab使用
1.查看.编辑和删除 cron把命令行保存在crontab(cron table)文件里,这个文件通常在 /etc 目录下. 每个系统用户都可以有自己的crontab(在 /var/spool/cro ...
- 【转载】树链剖分.By.Xminh
轻重链剖分 其实就是俗称的树链剖分. PS:树链剖分不止有轻重链剖分.但是大多数时候的树链剖分指的就是轻重链剖分. dfs序 给树的节点重新编号,使得任意一个节点满足子树的dfs序都比它要大,而且它子 ...
- vc 自删除
// delself.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <windows.h>#include &l ...
- 利用虚拟网桥实现Docker容器的跨主机访问
最近在研究Docker,Docker的网络配置是比较令人头疼的部分,尤其是跨主机的容器间通信,很多解决方案都比较复杂,这里,我只用虚拟网桥来实现Docker的跨主机访问,分享出来,希望对Docker学 ...
- python中的字符串的种种函数
1.连接list:为了将任意包含字符串的 list 连接成单个字符串,可以使用字符串对象的 join 方法. join 只能用于元素是字符串的 list:它不进行任何的强制类型转换.连接一个存在一个或 ...
- java:内存处理ByteArrayOutputStream,ByteArrayInputStream
//用内存,将小写字母替换成大写字母 String str = "helloworld,goodmorning"; ByteArrayOutputStream bos = null ...