自己动手写写:HashSet、LinkedHashSet源码浅析
这篇文章我只是作为一个简要的分析。
?
首先可以看看之前写的两篇的博文,只要你熟悉了下面这两个类的源码就显得很简单了!
?
先来介绍下HashSet吧!
/** * Constructs a new, empty linked hash set. (This package private * constructor is only used by LinkedHashSet.) The backing * HashMap instance is a LinkedHashMap with the specified initial * capacity and the specified load factor. * * @param initialCapacity the initial capacity of the hash map * @param loadFactor the load factor of the hash map * @param dummy ignored (distinguishes this * constructor from other int, float constructor.) * @throws IllegalArgumentException if the initial capacity is less * than zero, or if the load factor is nonpositive */ HashSet(int initialCapacity, float loadFactor, boolean dummy) { map = new LinkedHashMap<E, Object>(initialCapacity, loadFactor); } ?
?此构造函数的访问权限是default,即同包下可见!LinkedHashSet就是通过此构造函数,通过维护这一个LinkedHashMap来实现的。