HashSet source code analysis: JDK source series

1 Introduction

Continue to analyze source code, an article on the completion of HashMap analysis. This article analyzes the beginning HashSet brief look.

HashSet element is a non-repeating set of internal HashMap implemented, so HashMap Y. features inherited down. The storage element is disordered and HashSet allows empty elements.

HashSet unsynchronized. If multiple threads access a hash set, and at least one of the threads modifies the set, it must be synchronized externally. (Reference JDK1.8 documentation, attention reply JDK available Chinese version of the JDK documentation)

 Set s = Collections.synchronizedSet(new HashSet(...)); 

The above link:

HashMap source code reading (a)

HashMap source code reading (b)

1. inheritance structure

Look at the inheritance structure of HashMap

HashSet source code analysis: JDK source series

And other collections, like HashSet also implements the Cloneable and Serializable two interfaces, but also to some of the Set interface Set interface specification.

  • Cloneable clone
  • Serialization Serializable

2 Properties

HashSet data storage is achieved by the HashMap, HashMap so some of the features are also inherited over. In reading the source code of the time do not go directly to the best first read the HashSet HashMap read before reading. When read in conjunction with the best HashMap 1.7 version of the source watching.

private transient HashMap<E,Object> map;

Speaking HashSet above data is stored by the HashMap implemented as a HashMap K, V unity is PRESENT

   // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();

3. Constructor

Constructor with no arguments, HashMap directly create a default attribute constructor with no arguments on the article said that the default capacity and load factor is 16 0.75

HashSet source code analysis: JDK source series

Specified capacity

HashSet source code analysis: JDK source series

Specified capacity and load factor, the load factor is used to calculate the total capacity of the capacity of the default * in the HashMap loading factor, the default load factor is 0.75

HashSet source code analysis: JDK source series

Elements of the specified collection

HashSet source code analysis: JDK source series

4. Add

HashSet source code analysis: JDK source series

It can be seen elements put to use HashSet add elements to be added as mapd the Key and the default value is PRESENT. The article introduced the HashMap put method if the inserted Key value does not exist, it returns null otherwise it returns the value already exists, so here made a judgment. Is not it simple.

5. Find

HashSet source code analysis: JDK source series

Find the element called HashMap containsKey method returns true if there is absence of returns false.

6. Delete

HashSet source code analysis: JDK source series

Delete method also calls the remove method map and see where we see HashSet is all dependent on the HashMap.

7. iterative method

HashSet source code analysis: JDK source series

Map also be achieved by using a key keySet to return the Iterator.

8. Conclusion

In fact, some of the things HashSet is implemented by HashMap, HashMap if the source code has been read, then basically no problem. (This is probably the most relaxed I wrote an article asking ha ha ha ha ha)

Guess you like

Origin blog.51cto.com/14465400/2423047