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 ...
随机推荐
- java 获取web登录者的ip地址
/** * 获取访问用户的客户端IP(适用于公网与局域网). */ public static final String getIpAddr(final HttpServletRequest requ ...
- Django学习之ORM练习题
一.表关系 创建表关系,并创建约束 班级表:class 学生表: student cid caption grade_id sid sname gender class_id 1 一年一班 1 1 乔 ...
- 完全取代VC上原有的view
如果需要在这个VC上放置一个subviewA,作用相当于取代self.view,那么最好不要使用 [self.view addSubView: subviewA]; 而要使用 self.view = ...
- python 中的 *args 和 **kwargs
在阅读Python代码时,经常会看到如下函数的定义: def fun(*args, **kwargs): 很多同学可能会对此感到困惑,这个 * args和 **kwargs是什么东西.为啥会在源码中应 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:制作一个小按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- postgresql shell脚本传递参数并执行sql脚本并
参考: https://stackoverflow.com/questions/7389416/postgresql-how-to-pass-parameters-from-command-line ...
- 新见Java数据类型_需了解
LinkedList<T>.LinkedList.poll() 先给出结论:pop 与 poll 都是取出 LinkedList 的第一个元素,并将该元素删除,等效于:removeFirs ...
- Spring--@configuration 和 @Bean
参考:http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html @Configuration 和 @Bean 注 ...
- Android示例程序剖析之记事本(一)
Android SDK提供了很多示例程序,从这些示例代码的阅读和试验中能够学习到很多知识.本系列就是要剖析Android记事本示例程序,用意就是一步步跟着实例进行动手操作,在实践中体会和学习Andro ...
- uWSGI 和 SQLAlchemy 一起使用的注意事项
最近在使用 Flask 中使用 SQLAlchemy 开发程序,一开始好好的,然后使用 uWSGI 部署到线上后,出现了各种 mysql 客户端的问题,如: (_mysql_exceptions.P ...