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有两 ...
随机推荐
- Go panic recover
panic 1. 停止当前函数执行 2. 一直向上返回,执行每一层的defer 3. 如果没有遇到recover, 程序退出 recover 1. 仅在defer调用中使用 2. 获取panic的值 ...
- Get Started with ASP.NET Web API 2 (C#)
https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutoria ...
- C#遍历指定文件夹中的所有文件
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryInfo NextFolder in ...
- 分布式技术 memcached
memcached 是一个高性能的分布式内存对象缓存系统,用于动态web应用,以减轻数据库负载,它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.memcache ...
- JavaWeb -- 邮件收发
smtp协议: telnet smtp.qq.com 25 ehlo kevin auth login eGlhbmdqaWU1NUBxcS5jb20= a2V2aW5feGlhbmc1NQ== ma ...
- Oracle通过PLSQL进行数据表之间的同步
昨天被要求拉取第三方oracle中的一个表数据,起初以为要导出表数据,然后再自己库中建个相同的表,然后导入数据,查过资料之后oracle可以通过dblink的方式同步表数据. 1.首先利用PLSQL工 ...
- js模板方法
模板方法模式,定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤.模板方法模式把不变行为搬到超类中,从而去除了子类中的重复代码 ...
- mysql中使用instr替换like
使用内部函数instr,可代替传统的like方式查询,并且速度更快. instr函数,第一个参数是字段,第二个参数是要查询的串,返回串的位置,第一个是1,如果没找到就是0. 实例: SELECT o. ...
- hzau 1207 Candies
1207: Candies Time Limit: 2 Sec Memory Limit: 1280 MBSubmit: 223 Solved: 31[Submit][Status][Web Bo ...
- uva10910 背包
https://vjudge.net/problem/UVA-10910 给出N,T,P,表示N门考试考了T分,问有多少种不同的得分方案,已知每门课的得分不低于P. 令f[i][j]表示考了i门得了j ...