My jdk source: LinkedHashSet class

1. Overview

The LinkedHashSet class is also a member of the set family, and the LinkedHashSet class is an implementation of the Set class, which is non-repetitive and has a predictable iteration order, that is, the order in which we insert.See Source Parsing for details on how to do this.

2. Source Code Resolution

1. Declarations of classes

public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable,

The LinkedHashSet class inherits from the HashSet class and implements the Set interface, Cloneable interface and Serializable interface. This structure is very similar to that of the ArrayList class. Let's take a closer look:

* Inherit the HashSet class and implement the parent's methods on your own (although not in the source, in case you do).

* Implement the Set interface to increase readability, to clearly see the implemented interface, and to reduce maintenance costs. If the HashSet class does not implement Set, the LinkedHashSet class will not be affected.

* The Cloneable interface is also a clone tag interface, indicating that this class can be cloned and that instances of this class can call the clone() method; instances of classes that do not implement the Cloneable interface call the clone() method with an error and are already defined in the Object class.

* The Serializable interface is a serialization markup interface, indicating that this class can be serialized into memory.The goal is to make classes persistent, such as over a network or locally stored, and to provide prerequisites for system distribution and heterogeneous deployment.

2. Constructors

    // Afferent Capacity and Load Factor
    public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
    // Incoming capacity only, load factor defaults to 0.75
    public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    // Use default capacity 16, default load factor 0.75
    public LinkedHashSet() {
        super(16, .75f, true);
    // Add all elements from collection c to LinkedHashSet
    // Curiously, the way you calculate capacity here has changed
    // The HashSet usesMath.max((int) (c.size()/.75f) + 1, 16)
    // That's a bit confusing. Is the author lazy?
    public LinkedHashSet(Collection<? extends E> c) {
        super(Math.max(2*c.size(), 11), .75f, true);

All constructions of the LinkedHashSet class call the constructions of the HashSet, which in turn calls the constructions of the LinkedHashMap class.The source code is as follows:

    //Constructor in HashSet class
    HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);

    //Constructor in LinkedHashMap class
    public LinkedHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        accessOrder = false;

This makes it clear that the LinkedHashSet class creates instances that follow the collation accessOrder is false in LinkedHashMap, that is, they are sorted in insertion order (the node is placed at the end when a new node is created).

3. Iterators

    // A divisible iterator, used primarily for multithreaded parallel iteration
    public Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED);

3. Summary

The LinkedHashSet class is a part of our understanding of the family tree of a large set of families. The important points of knowledge of the LinkedHashSet class are as follows:

1. The underlying LinkedHashSet uses LinkedHashMap to store elements.

2. LinkedHashSet is ordered. It is sorted by insertion order and does not support sorting by element access order.

Please look forward to My jdk Source (19): TreeMap.

For more exciting content, please scan the QR code below and follow my WeChat Public Number for the first update!

Tags: Java network JDK

Posted on Sun, 07 Jun 2020 12:28:17 -0400 by woobarb