HashSet LinkedHashSet source analysis and analysis of the HashMap JDK1.8 source (a) analysis of a LinkedHashMap JDK1.8 source (b) Analysis of a LinkedHashMap JDK1.8 source (b)

I. Introduction

  After analyzing the finished List two major classes, we have to analyze the Set interface classes under, HashSet and LinkedHashSet, in fact, after the completion of the analysis HashMap and LinkedHashMap, let's analyze HashSet and LinkedHashSet, it will become very simple, begin the analysis below .

Second, the data structure

  2.1 HashSet data structure

  Old rules, the first data structure, because the bottom is HashSet HashMap or LinkedHashMap based implementation, the data structure is HashSet HashMap or LinkedHashMap data structure, as has already been analyzed, so no cumbersome. Direct look here, the HashMap JDK1.8 source code analysis of (a) &&  JDK1.8 source code analysis of a LinkedHashMap (II) .

  2.2 LinkedHashSet data structure

  LinkedHashSet based LinkedHashMap implemented, the data structures directly see here. JDK1.8 source code analysis of LinkedHashMap (two) .

Third, source code analysis

  3.1 HashSet  

  1. class inheritance

public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable

  2. The class of property 

Copy the code
public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
    // version serial number
    static final long serialVersionUID = -5024744406713321676L;
    // key Map
    private transient HashMap<E,Object> map;
    // used as a value corresponding to all the keys, the key values ​​are equal to corresponding
    private static final Object PRESENT = new Object();
}
Copy the code

  Description: HashSet due contains only key, does not contain a value, since the underlying specific implementation, the use or a LinkedHashMap HashMap (constructors can be specified to determine which structure to use), we know that the key is stored HashMap, so in order to adaptation storage HashMap, HashSet PRESENT adds a class field (all category), all the keys have the same value (PRESENT).

  3. Other analysis

  add, contains, remove or operating functions are based HashMap do LinkedHashMap, source code analysis have been previously given, not cumbersome.

  3.2 LinkedHashSet

  1. class inheritance  

public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable, java.io.Serializable

  Description LinkedHashSet inherited from HashSet, also implements a number of interfaces, not cumbersome.

  2. Other notes

  LinkedHashSet will call the parent class constructor HashSet, let the underlying implementation is LinkedHashMap, so to achieve a good functional LinkedHashSet need.

IV Summary

  HashSet, LinkedHashSet and HashMap, LinkedHashMap, respectively. After analyzing the HashMap, LinkedHashMap source, HashSet, LinkedHashSet also a very easy to understand. Thank you Friends of the Park Watch ~

Guess you like

Origin www.cnblogs.com/wzq-xf/p/11784611.html