Java容器类框架分析(6)LinkedHashSet源码分析

在分析LinkedHashSet的时候,先看一下它的继承关系安全

LinkedHashSet的继承关系
LinkedHashSet的继承关系

能够看到LinkedHash继承自HashSet,即拥有HashSet的所有属性,接着来看一下源码中的注释bash

  • Hash table and linked list implementation of the Set interface,
    with predictable iteration order. This implementation differs from
    HashSet in that it maintains a doubly-linked list running through
    all of its entries. This linked list defines the iteration ordering,
    which is the order in which elements were inserted into the set
    (insertion-order). Note that insertion order is not affected
    if an element is re-inserted into the set.
  • 这个类是实现了Set接口的哈希表跟链表。此实现类跟HashSet的区别在于它内部持有一个双链表而且存储了全部的entry。这个链表定义了迭代的顺序,该顺序就是元素被插入Set的顺序。注意若是一个元素被重复插入,迭代顺序是不会被影响的。

看到这里,感受其实跟HashSet一个套路,底层都不是本身实现的,而是经过内部实现的一个LinkedHashMap来维护的,所谓HashSet取地是HashMap的key,HashSet取地是LinkedHashMap的Key,下面从源码的角度来验证一下。ui

正文

成员变量

就一个序列化Id,其他的所有继承自HashSetspa

private static final long serialVersionUID = -2851667679971038690L;复制代码

构造方法

public LinkedHashSet() {
        super(16, .75f, true);
    }
      public LinkedHashSet() {
        super(16, .75f, true);
    }
     public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }

    public LinkedHashSet() {
        super(16, .75f, true);
    }复制代码

能够发现,无论是调用哪个构造方法,最后调用的都是调用的同一个父类方法,也就是上一篇HashSet中的一个LinkedHashMap的初始化方法,最终仍是初始化了一个LinkedHashMap线程

HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
    }复制代码

Over

当我还想继续查看的时候,发现其实已经没有能够分析的了,没有复写HashSet的其它方法,因此,LinkedHashSet内部没有作过不少的实现,只是调用了HashSet的初始化LinkedHashMap的构造方法,剩余的操做跟HashSet其实是同样的,没有什么区别。code

总结

  1. LinkedHashSet中的LinkedHash跟LinkedHashMap中的LinkedHash其实是同样的,哈希表跟链表
  2. LinkedHashSet跟HashSet都是非线程安全的,不容许重复元素
  3. LinkedHashSet都容许插入null,插入的元素须要复写hashcode跟equals方法
相关文章
相关标签/搜索