读书人

自各儿动手写写:HashSet、LinkedHash

发布时间: 2012-10-30 16:13:36 作者: rapoo

自己动手写写:HashSet、LinkedHashSet源码浅析

这篇文章我只是作为一个简要的分析。

?

首先可以看看之前写的两篇的博文,只要你熟悉了下面这两个类的源码就显得很简单了!

自己动手写写:HashMap源码浅析

自己动手写写:LinkedHashMap源码浅析

?

先来介绍下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来实现的。

读书人网 >编程

热点推荐