The interviewer in Ali asked me HashSet, and I talked to him for an hour!

There are no useless gears in this world, and only the gears themselves can determine their purpose.
——Kyogo Higashino "Devotion of Suspect X"

0 Preface

HashSet is a collection without duplicate elements. It is mainly implemented by HashMap, which does not guarantee the order of elements, but allows null elements. It is not thread-safe. If you need security, please lock it yourself, or use Collections.synchronizedSet. It is best to complete this at creation An operation to prevent accidental unsynchronized access to the set.

1 Inheritance system

HashSet inherits from AbstractSet and implements the Set interface. The essence of HashSet is a "no duplicate elements" collection, which is implemented through HashMap. HashSet contains a "member variable of type HashMap" map, the operation function of HashSet is actually realized by map.

2 Properties

  • HashSet combines HashMap and treats HashMap as a local variable

It can be extended on the basis of the basic class methods, and the method naming can be arbitrarily named, without having to be consistent with the method name of the basic class.

  • The virtual value associated with the object in the backup map

3 Construction method

3.1 No parameters

  • Direct new HashMap

3.2 Participation

  • When the original collection data is initialized

Calculate the capacity of HashMap: take the maximum value of the two numbers in parentheses.
(期望值 / 0.75+1,默认值 16)
If the initial capacity of a given HashMap is
< 16 initialized
> 16 according to the default 16 of HashMap, initialize it according to the given value

HashMap expansion threshold: Map capacity * 0.75f, and the calculated value here is exactly 1 greater than the threshold, it will not be expanded immediately.

The APIs of HashSet are relatively simple, that is, the HashMap is simply wrapped. Let ’s take a look at a few,

4 add (E e)

  • Use the put method of HashMap directly and judge

If the specified element does not already exist, it is added to the set. More precisely

  • If this element set does not contain any element e2,
    (e==null ? e2==null : e.equals(e2))
    then add the specified element e to the element set
  • If this collection already contains the element, the call will leave the collection unchanged and return false

5 remove

  • Directly call the remove method of HashMap and judge

If it exists, delete the specified element from this set. More precisely, if this set contains such element
(o==null ? e==null : o.equals(e))
, delete the element.
If this set contains this element (or, if this set is changed due to a method call), It returns true. (Once the call returns, this collection will not contain the element)

The implementation of other methods is similar to this, and will not be described in detail.

6 Summary

The implementation of HashSet leaves us with some best practices

  • Combination is sometimes more applicable than inheritance
  • Pay attention to encapsulating complex logic to make the external interface full of user experience
  • It is necessary to have a good understanding of the other data structures of the combination in order to make the code design achieve 1 + 1> 2 strange effects.
437 original articles were published · 1317 praises · 630,000 + views

Guess you like

Origin blog.csdn.net/qq_33589510/article/details/105463870