Rising Sun

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  148 随笔 :: 0 文章 :: 22 评论 :: 0 Trackbacks
Hashmap loadFactor设置初使化值的时候必须设置 loadFactor

1、请查看 hashmap 如何初使化

 

代码:

 public HashMap(int initialCapacity, float loadFactor) {

        if (initialCapacity < 0)

            throw new IllegalArgumentException("Illegal initial capacity: " +

                                               initialCapacity);

        if (initialCapacity > MAXIMUM_CAPACITY)

            initialCapacity = MAXIMUM_CAPACITY;

        if (loadFactor <= 0 || Float.isNaN(loadFactor))

            throw new IllegalArgumentException("Illegal load factor: " +

                                               loadFactor);

 

        // Find a power of 2 >= initialCapacity

        int capacity = 1;

        while (capacity < initialCapacity)

            capacity <<= 1;

 

        this.loadFactor = loadFactor;

        threshold = (int)(capacity * loadFactor);

        table = new Entry[capacity];

        init();

}

 

Map map = new HashMap(9);

 

while (capacity < initialCapacity)

            capacity <<= 1;

 

capacity的容量为 16

threshold扩容阀值 16*0.75 =12

 

和9没什么关系 哈

 

Map map = new HashMap(91);

 

capacity的容量为 16

threshold扩容阀值 16*1 =16
不过这个 9 变小一点 就不一样了 如3时
capacity 是4



 

posted on 2013-02-01 16:37 brock 阅读(2911) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: