Java8集合框架——LinkedHashSet源码分析
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java8集合框架——LinkedHashSet源码分析,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2574字,纯文字阅读大概需要4分钟。
内容图文
本文的目录结构如下:
一、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集合框架——LinkedHashSet源码分析所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。