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. 写makefile时候的cc和gcc

    Linux 下 的 cc 和 gcc     Linux 下 的 cc 和 gcc 周银辉 在Linux下一会看到cc,另一会又看到gcc,感觉又点混乱的样子.它们是同一个东西么,有啥区别呢 一分为二 ...

  2. tcp底层连接过程(c语言)

    在用了多种上位机开发环境,包括mfc.Qt.C#之后,发现它们的API都是对底层协议的(可以说是C语言)的封装,所以了解了底层协议,任意换上位机开发环境都是没问题的. 1.服务器创建套接字socket ...

  3. 16个tomcat面试题

    1)解释什么是Jasper? Jasper是Tomcat的JSP引擎 它解析JSP文件,将它们编译成JAVA代码作为servlet 在运行时,Jasper允许自动检测JSP文件的更改并重新编译它们 2 ...

  4. jprofile查看hprof文件[转]

    用jprofile打开hprof文件,查看内存泄露情况,有几个常用的功能说明一下: 工具下载:到官网下载jprofile7.0.1 64位的.再申请一个注册号,注册号的申请好像是一个邮件只能用一次. ...

  5. sqlserver 实时同步(发布订阅)

    配置发布订阅手册 不同版本须知:https://www.sqlmanager.net/en/articles/1548 向后兼容性:参考https://docs.microsoft.com/zh-cn ...

  6. Windows下MetaMap工具安装

    Windows下MetaMap工具安装 一.Main MetaMap安装 Prerequisties 12G磁盘空间 JAVA6 or newer JRE or SDK installed Downl ...

  7. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  8. Java中interface是否继承Object类

    首先我们从C++说起, c++可以多继承.也就是一个类型 --- class,可以继承自2个以上的父类型.多继承导致一个问题,很多人知道.例如,如果类型B,类型C均继承自类型A.然后类型D继承自类型B ...

  9. js中出现问题--Type Syntax error on token "catch", Identifier expected jquery.js

    解决方案: 1.选中jQuery报错的web工程: 2.右键-->Myeclipse-->Exclude From Validation,选中: 3.继续右键Myeclipse--> ...

  10. Redis 高可用及分片集群,说了你也不懂

    Redis 简介 Memcached: 优点:高性能读写.单一数据类型.支持客户端式分布式集群.一致性hash 多核结构.多线程读写性能高. 缺点:无持久化.节点故障可能出现缓存穿透.分布式需要客户端 ...