JDK 8 Class HashSet<E> Doc:

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 实现机制分析的更多相关文章

  1. JDK 8 - java.util.HashMap 实现机制分析

    官方文档对 HashMap 的定义: public class HashMap<K,V> extends AbstractMap<K,V> implements Map< ...

  2. java.util.HashSet源码分析

    public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java. ...

  3. 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> ...

  4. 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> ...

  5. 原子类java.util.concurrent.atomic.*原理分析

    原子类java.util.concurrent.atomic.*原理分析 在并发编程下,原子操作类的应用可以说是无处不在的.为解决线程安全的读写提供了很大的便利. 原子类保证原子的两个关键的点就是:可 ...

  6. java.util.concurrent各组件分析 一 sun.misc.Unsafe

    java.util.concurrent各组件分析 一 sun.misc.Unsafe 说到concurrent包也叫并发包,该包下主要是线程操作,方便的进行并发编程,提到并发那么锁自然是不可缺少的, ...

  7. java.util.concurrent包详细分析--转

    原文地址:http://blog.csdn.net/windsunmoon/article/details/36903901 概述 Java.util.concurrent 包含许多线程安全.测试良好 ...

  8. java.util.Collection源码分析和深度讲解

    写在开头 java.util.Collection 作为Java开发最常用的接口之一,我们经常使用,今天我带大家一起研究一下Collection接口,希望对大家以后的编程以及系统设计能有所帮助,本文所 ...

  9. java.util.Hashtable源码分析

    Hashtable实现一个键值映射的表.任何非null的object可以用作key和value. 为了能存取对象,放在表里的对象必须实现hashCode和equals方法. 一个Hashtable有两 ...

随机推荐

  1. photoshop cs5 序列号永久序列号永久激活破解方法

    photoshop cs5 序列号永久序列号永久激活破解方法 (2016-12-10 07:52:21) 转载▼ 标签: it   PhotoShop CS5 /ps5  序列号激活码 1330-15 ...

  2. 【bzoj1925】地精部落[SDOI2010](dp)

    题目传送门:1925: [Sdoi2010]地精部落 这道题,,,首先可以一眼看出他是要我们求由1~n的排列组成,并且抖来抖去的序列的方案数.然后再看一眼数据范围,,,似乎是O(n^2)的dp?然后各 ...

  3. 智能穿戴设备移动APP端与外设数据传输协议

    S1 Communication Layer specifications 1. Purpose of This Document                                    ...

  4. 利用PushbackReader读取文件中某个字符串之前的内容

    package File; import java.io.FileReader; import java.io.IOException; import java.io.PushbackReader; ...

  5. javaScript中的DOM补充

    一.DOM树 二.DOM节点 DOM 是这样规定的:    整个文档是一个文档节点     每个 HTML 标签是一个元素节点     包含在 HTML 元素中的文本是文本节点     每一个 HTM ...

  6. Teamviewer_相关

    1.官网下载:https://www.teamviewer.com/zhcn/download/windows/,里面选择 "Portable"的版本来下载(按钮"下载P ...

  7. OpenStack with Opendaylight Part 1: Intro to Pipeline

    Using Vagrant to create vm nodes; devstack to start openstack using Opendaylight as ML2. Openstack w ...

  8. xenapi-add-support-for-vgpu

    Model types 为了在vm中支持vgpu,问题在于vgpu是否应该被当作一个PCI设备, 虽然物理GPU不是一个SR-IOV设备,但vgpu却是以一个类似SR-IOV device上的VF赋给 ...

  9. php特级课---1、网站大访问量如何解决

    php特级课---1.网站大访问量如何解决 一.总结 一句话总结: 负载均衡和冗余技术 1.负载均衡和冗余技术是一回事么? 并不是:负载均衡是用户分流:冗余技术是避免出现单点故障 负载均衡:将不同的用 ...

  10. 搭建svn服务器小结

    最开始是用VisualSVN-Server来搭建服务器的,某一天安装了eset杀毒软件后,发现不能用了,每次打开配置页时报错.Visualsvn server 0x80070005 百度搜索,找到相同 ...