首页 > 编程语言 > 详细

java Hashmap

时间:2015-04-03 15:31:11      阅读:137      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!