一.介绍

1.HashSet

2.HashSet和HashMap的区别

1.相同点

  • HashSet 内部使用 HashMap 存储元素,对应的键值对的键为 Set 的存储元素,值为一个默认的 Object 对象。
  • HashSet和HashMap都是采用Hash算法来决定其元素的存储
  • 都不是同步的

2.不同点

  • HashSet实现了Set接口

    HashMap实现了Map接口

3.fast-fail

  • 快速失败(fail—fast)在用迭代器遍历一个集合对象时,如果遍历过程中集合对象中的内容发生了修改(增加、删除、修改),则会抛出ConcurrentModificationException
  • 迭代器在遍历时直接访问集合中的内容,并且在遍历过程中使用一个 modCount 变量。集合在被遍历期间如果内容发生变化,就会改变 modCount 的值。每当迭代器使用hasNext()/next() 遍历下一个元素之前,都会检测 modCount 变量是否为expectedmodCount 值,是的话就返回遍历值;否则抛出异常,终止遍历。
  • java.util包下的集合类都是快速失败的,不能在多线程下发生并发修改(迭代过程中被修改)。

二.源码部分

1.基本属性

继承AbstractSet:它实现了Set接口中的大部分函数。从而方便其它类实现Set接口。

Cloneable是标记型的接口,它们内部都没有方法和属性,实现 Cloneable来表示该对象能被克隆

Set 接口中的方法和 Collection 中方法一致的。Set 接口取出方式只有一种, 迭代器 。

java.io.Serializable接口:

可以启用其序列化功能。未实现次接口的类无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。

public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
//定义序列化ID
static final long serialVersionUID = -5024744406713321676L;
//存储数据的map
private transient HashMap<E,Object> map; // Dummy value to associate with an Object in the backing Map 与后台映射中的对象关联的虚拟值
private static final Object PRESENT = new Object();

2.构造函数

    /**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
* 构造一个新的空集;HashMap实例具有默认的初始容量(16)和负载因子(0.75)。
*/
public HashSet() {
map = new HashMap<>();
}
    /**
* Constructs a new set containing the elements in the specified collection.
* 构造包含指定集合中的元素的新集合
* The <tt>HashMap</tt> is created with default load factor
* (0.75) and an initial capacity sufficient to contain the elements in
* the specified collection.
* 创建的HashMap带有默认负载因子(0.75)和初始容量,初始容量足以包含指定集合中的元素。
* @param c the collection whose elements are to be placed into this set
* @throws NullPointerException if the specified collection is null
*/
public HashSet(Collection<? extends E> c) {
//?
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
//将集合c加入
addAll(c);
}
    /**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* the specified initial capacity and the specified load factor.
* 建造指定容量和负载因子的空集合
* @param initialCapacity the initial capacity of the hash map
* @param loadFactor the load factor of the hash map
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
    /**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* the specified initial capacity and default load factor (0.75).
* 构造指定初始容量的集合
* @param initialCapacity the initial capacity of the hash table
* @throws IllegalArgumentException if the initial capacity is less
* than zero
*/
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
//构造的是LinkedHashMap,该方法不对外公开,是提供给LinkedHashSet使用的
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}

3.contains(Object o)

    public boolean contains(Object o) {
return map.containsKey(o);
}

4.add(E e)

    public boolean add(E e) {
return map.put(e, PRESENT)==null;
}

5.remove(Object o)

public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}

三.总结

  1. HashSet怎么不重复?
  • 通过存储元素的 hashCode 方法和 equals 方法来确定元素是否重复。

HashSet源码学习的更多相关文章

  1. HashSet源码学习,基于HashMap实现

    HashSet源码学习 一).Set集合的主要使用类 1). HashSet 基于对HashMap的封装 2). LinkedHashSet 基于对LinkedHashSet的封装 3). TreeS ...

  2. Dubbo源码学习--注册中心分析

    相关文章: Dubbo源码学习--服务是如何发布的 Dubbo源码学习--服务是如何引用的 注册中心 关于注册中心,Dubbo提供了多个实现方式,有比较成熟的使用zookeeper 和 redis 的 ...

  3. Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件

    写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...

  4. 死磕Java之聊聊HashSet源码(基于JDK1.8)

    HashSet的UML图 HashSet的成员变量及其含义 public class HashSet<E> extends AbstractSet<E> implements ...

  5. mybatis源码学习:插件定义+执行流程责任链

    目录 一.自定义插件流程 二.测试插件 三.源码分析 1.inteceptor在Configuration中的注册 2.基于责任链的设计模式 3.基于动态代理的plugin 4.拦截方法的interc ...

  6. Spring5.0源码学习系列之浅谈BeanFactory创建

    Spring5.0源码学习系列之浅谈BeanFactory创建过程 系列文章目录 提示:Spring源码学习专栏链接 @ 目录 系列文章目录 博客前言介绍 一.获取BeanFactory主流程 二.r ...

  7. Spring5.0源码学习系列之浅谈循环依赖问题

    前言介绍 附录:Spring源码学习专栏 在上一章的学习中,我们对Bean的创建有了一个粗略的了解,接着本文浅谈Spring循环依赖问题,这是一个面试比较常见的问题 1.什么是循环依赖? 所谓的循环依 ...

  8. Java并发包源码学习系列:线程池ThreadPoolExecutor源码解析

    目录 ThreadPoolExecutor概述 线程池解决的优点 线程池处理流程 创建线程池 重要常量及字段 线程池的五种状态及转换 ThreadPoolExecutor构造参数及参数意义 Work类 ...

  9. JUC源码学习笔记5——线程池,FutureTask,Executor框架源码解析

    JUC源码学习笔记5--线程池,FutureTask,Executor框架源码解析 源码基于JDK8 参考了美团技术博客 https://tech.meituan.com/2020/04/02/jav ...

随机推荐

  1. sublime text 3 添加packagecontrol

    打开sublime控制台输入 import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee1 ...

  2. 使用.NET 6开发TodoList应用(23)——实现请求限流

    系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 Rate Limiting允许保护我们的API服务免受过多请求的连接导致的性能下降,如果请求次数超过了限制,API服务端将会拒 ...

  3. 反射获取到class文件中的实例变量

    获取类的class 属性的三种方式 1.对象获取: 调用person类的父类方法getClaass(); Person p = new Person(); Class c = p.getClaass( ...

  4. 2022年form表单中input控件最详细总结

    语法 <input type="" name="" id="" value="" placeholder=&quo ...

  5. Genymotion模拟器配置与使用

    1.https://www.genymotion.com网站注册模拟器账号,并按照指引激活账号 此过程中提示商业用还是个人使用,商业使用需要有注册码,个人使用不需要注册码,我选择的是个人使用. 2.激 ...

  6. 【刷题-LeetCode】209. Minimum Size Subarray Sum

    Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...

  7. 【记录一个问题】用ndk的gcc命令行无法编译C++11的lambda等语法的代码

    /Users/ahfu/code/android/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_6 ...

  8. http 的get 与 post 的区别

    1.原理区别 一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Http定义了与服务器交互的不同方法,最基本的 ...

  9. 返回值Student处理过程

  10. hexo博客如何插入图片

    Hexo是一个静态的博客网站生成器,生成一个博客只需要分分钟的时间就能搞定. Hexo的博文是支持Markdown格式的,发表一篇文章只需要简简单单的几个命令. hexo new '文章'就会生成一个 ...