码迷,mamicode.com
首页 > 编程语言 > 详细

java Hashmap

时间:2015-04-03 15:34:07      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:hashmap

hashmap的key值可以为空。初始默认16的容量,阈值为0.75,即达到12的时候就要扩容;

void addEntry(int hash, K key, V value, int bucketIndex) { 
Entry<K,V> e = table[bucketIndex]; 
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e); 
        if (size++ >= threshold) 
            resize(2 * table.length); 
    } 

public V put(K key, V value) { 
        if (key == null) 
            return putForNullKey(value); 
        int hash = hash(key.hashCode()); 
        int i = indexFor(hash, table.length); 
        for (Entry<K,V> e = table[i]; e != null; e = e.next) { 
            Object k; 
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { 
                V oldValue = e.value; 
                e.value = value; 
                e.recordAccess(this); 
                return oldValue; 
            } 
        } 
        modCount++; 
        addEntry(hash, key, value, i); 
        return null; 
    }

可以看出,不冲突的情况下,插入为O(1);并且size达到阈值后将会扩容到以前的2倍!

关于hashmap的一篇好文章:http://blog.csdn.net/ghsau/article/details/16843543

java Hashmap

标签:hashmap

原文地址:http://blog.csdn.net/j754379117/article/details/44853465

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!