Java8集合框架——LinkedHashSet源码分析
时间:2020-01-18
本文章向大家介绍Java8集合框架——LinkedHashSet源码分析,主要包括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 实例在操作,保证有序。不再赘述。
原文地址:https://www.cnblogs.com/wpbxin/p/12209536.html
- 【译】使用Apache的mod重写来保护你的C2 Empire
- 大白话,设计一个购物车对象
- 【译】Cromos – 下载并注入代码到谷歌 Chrome 浏览器扩展中
- X-NUCA 2017第三期 WriteUp
- 学习分享 | Flipped Ciphertext Bits
- 聊一下JavaScript定时器
- java redis 通用组建
- 学习分享 | Padding Oracle
- FastJson 反序列化注意事项
- Python编写渗透工具学习笔记二 | 0x05编写脚本劫持tcp会话
- linux下socket编程
- java与openssl的rsa算法互
- Python编写渗透工具学习笔记二 | 0x04编写程序分析流量检测ddos攻击
- Python编写渗透工具学习笔记二 | 0x03用python构建ssh僵尸网络
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- RoarCTF2019-EasyJava
- ACTF2020新生赛-BackupFile
- DB2 Linux平台安装 Part 5 数据库常见参数配置
- GXYCTF2019-PingPingPing
- 利用cloudflareWorkers制作Google镜像站
- MySQL information_schema详解 COLLATIONS
- 极客大挑战2019-Knife
- DB2备份还原相关操作
- MySQL复制全解析 Part 8 MySQL Auto-Positioning
- Redis 5.0.8 主从搭建
- Linux 修改网卡名称及mac地址
- MySQL Galera Cluster全解析 Part 10 grastate.dat文件详解
- MySQL MHA部署 Part 7 MHA手动切换测试
- 使用sysbench进行压测 Part2 sysbench语法介绍
- mysqlbinlog命令详解 Part 1-实验环境准备