Java8集合框架——LinkedHashSet源码分析
本文的目录结构如下:
一、LinkedHashSet 的 Javadoc 文档注释和简要说明
还是以官方 javadoc 作为参考进行说明:
- LinkedHashSet 是 Set 接口的 hash table 和 linked list 实现,而且迭代顺序可预测(按照元素的插入顺序),实际上 LinkedHashSet 继承了 HashSet,内部使用了 LinkedHashMap 实例,共用一个 value;和 HashSet 的不同之处在于维护了双链表;对于需要保持有序的 Set 参数的场景很实用。
- 允许存储 null;迭代/遍历的效率也只是和实际元素的个数有关。
- LinkedHashSet 也是非线程安全的,需要其他的工具类来保证线程安全。
- LinkedHashSet 也是 fail-fast;同样也并不保证出现有并发修改就百分百抛出 ConcurrentModificationException。

二、LinkedHashSet 的内部实现:构造函数
LinkedHashSet 没有扩展的属性,直接继承了 HashSet。构造函数都是通过 HashSet 的包级私有构造函数来返回 LinkedHashMap 实例。
/**
* Constructs a new, empty linked hash set with the specified initial
* capacity and load factor.
* 设置 初始容量 和 负载因子
* @param initialCapacity the initial capacity of the linked hash set
* @param loadFactor the load factor of the linked hash set
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
} /**
* Constructs a new, empty linked hash set with the specified initial
* capacity and the default load factor (0.75).
* 设置 初始容量 和 默认负载因子
* @param initialCapacity the initial capacity of the LinkedHashSet
* @throws IllegalArgumentException if the initial capacity is less
* than zero
*/
public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true);
} /**
* Constructs a new, empty linked hash set with the default initial
* capacity (16) and load factor (0.75).
* 设置 默认初始容量 和 默认负载因子
*/
public LinkedHashSet() {
super(16, .75f, true);
} /**
* Constructs a new linked hash set with the same elements as the
* specified collection. The linked hash set is created with an initial
* capacity sufficient to hold the elements in the specified collection
* and the default load factor (0.75).
*
* @param c the collection whose elements are to be placed into
* this set
* @throws NullPointerException if the specified collection is null
*/
public LinkedHashSet(Collection<? extends E> c) {
super(Math.max(2*c.size(), 11), .75f, true);
addAll(c);
}
三、LinkedHashSet 的 add 操作和 remove 操作
和 HashSet 一致,只是内部是 LinkedHashMap 实例在操作,保证有序。不再赘述。
Java8集合框架——LinkedHashSet源码分析的更多相关文章
- Java8集合框架——LinkedList源码分析
java.util.LinkedList 本文的主要目录结构: 一.LinkedList的特点及与ArrayList的比较 二.LinkedList的内部实现 三.LinkedList添加元素 四.L ...
- Java8集合框架——ArrayList源码分析
java.util.ArrayList 以下为主要介绍要点,从 Java 8 出发: 一.ArrayList的特点概述 二.ArrayList的内部实现:从内部属性和构造函数说起 三.ArrayLis ...
- Java8集合框架——LinkedHashMap源码分析
本文的结构如下: 一.LinkedHashMap 的 Javadoc 文档注释和简要说明 二.LinkedHashMap 的内部实现:一些扩展属性和构造函数 三.LinkedHashMap 的 put ...
- Java8集合框架——HashMap源码分析
java.util.HashMap 本文目录: 一.HashMap 的特点概述和说明 二.HashMap 的内部实现:从内部属性和构造函数说起 三.HashMap 的 put 操作 四.HashMap ...
- Java8集合框架——HashSet源码分析
本文的目录结构: 一.HashSet 的 Javadoc 文档注释和简要说明 二.HashSet 的内部实现:内部属性和构造函数 三.HashSet 的 add 操作和扩容 四.HashSet 的 r ...
- 死磕 java集合之LinkedHashSet源码分析
问题 (1)LinkedHashSet的底层使用什么存储元素? (2)LinkedHashSet与HashSet有什么不同? (3)LinkedHashSet是有序的吗? (4)LinkedHashS ...
- Java集合之LinkedHashSet源码分析
1.简介 我们知道Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false.根据源码实现中的注释我们可以知道LinkedHashSet是具有可预知迭代顺序的Set接 ...
- Java基础-集合框架-ArrayList源码分析
一.JDK中ArrayList是如何实现的 1.先看下ArrayList从上而下的层次图: 说明: 从图中可以看出,ArrayList只是最下层的实现类,集合的规则和扩展都是AbstractList. ...
- 框架-spring源码分析(一)
框架-spring源码分析(一) 参考: https://www.cnblogs.com/heavenyes/p/3933642.html http://www.cnblogs.com/BINGJJF ...
随机推荐
- Windows的本地时间(LocalTime)、系统时间(SystemTime)、格林威治时间(UTC-Time)、文件时间(FileTime)之间的转换
今天处理了一个Bug,创建历史数据时脚本函数的起始时间不赋值或者赋0值时,计算引擎推给历史库的UTC时间为-288000000000,一开始以为是bug,经过分析后发现不赋值默认给起始时间赋0值,而此 ...
- redis 初识与安装
一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的values类型相对更多,包括字符串.列表.哈希散列表.集合,有序集合. 这些数据类型都支持pus ...
- CSS盒模型的组成部分及实际大小
盒模型的组成? 盒模型由内容区域.内边距.边框.外边距四部分组成. 盒模型实际大小? 盒模型实际大小由内容区域.内边距.边框三部分组成. 盒模型的box-sizing属性? 如果对盒子不进行设置(或者 ...
- Decimal为SQL Server、MySql等数据库的一种数据类型
Decimal为SQL Server.MySql等数据库的一种数据类型,不属于浮点数类型,可以在定义时划定整数部份以及小数部分的位数.使用精确小数类型不仅能够保证数据计算更为精确,还可以节省储存空间, ...
- python爬虫入门之快递查询
现在快递遍布生活的角角落落,一个快递其实是信息的集合体,里面包含大量的物流信息,那能不能自己实现一个快递查询的小功能?答案是能的!现在也有别人整理好的快递查询api,比如说快递100,可以通过它提供的 ...
- kafka在zookeeper默认使用/为根目录,将/更换为/kafka
需求:kafka在zookeeper默认使用/为根目录,将/更换为/kafka 步骤:1.进入kafka的根目录: [root@node01 kafka_2.11-1.0.0]# cd /export ...
- 从0开始完成SpringBoot+Mybatis实现增删改查
1.准备知识: 1)需要掌握的知识: Java基础,JavaWeb开发基础,Spring基础(没有Spring的基础也可以,接触过Spring最好),ajax,Jquery,Mybatis. 2)项目 ...
- mabatis--使用mapper代理开发dao
1.编写mapper.xml映射文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE m ...
- java实现在线预览 - -之poi实现word、excel、ppt转html
简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服务就可以了,例如永中office.office web 365(http://w ...
- $.ajax方法提交数组参数
springmvc框架 var param = new Object(); var arr = new Array(); arr.push(1,2,3); param.ids=JSON.stringi ...