Java集合框架:了解LinkedHashSet

LinkedHashSet

基于LinkedHashMap实现的树集

目录

LinkedHashSet继承关系

LinkedHashSet源码分析 

LinkedHashSet总结


LinkedHashSet继承关系

LinkedHashSet实现了Serializable接口,支持序列化,可通过序列化传输

LinkedHashSet实现了Cloneable接口,覆盖了clone()方法,能被克隆

LinkedHashSet继承了HashSet类,有HashSet的相关操作方法

LinkedHashSet源码分析 

LinkedHashSet构造函数:

/**
 * 使用指定的初始容量和负载因子构造新的空链接散列集
 *
 * @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);
}

/**
 * 使用指定的初始容量和默认负载因子(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);
}

/**
 * 用默认的初始容量(16)和负载系数(0.75)构造一个新的空的链接哈希集
 */
public LinkedHashSet() {
    super(16, .75f, true);
}

/**
 * 使用与指定集合相同的元素和默认的负载因子(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总结

LinkedHashSet底层通过维护一个LinkedHashMap实现

LinkedHashSet初始的默认容量=16,默认负载因子=0.75

LinkedHashSet保证数据的唯一性和有序性(插入有序,存储无序)

LinkedHashSet是线程不安全的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值