zoukankan      html  css  js  c++  java
  • 深入HashMap

    HashMap:

    内部基于数组和单向链表

    重要的变量有:

    Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;结点数组table中存储的元素为链表的头结点。

    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;  数组table的初始化容量为16

    int size;  size指HashMap中键值对的实际个数

    final float loadFactor;  加载因子,默认0.75,

    int threshold;  指阀值,当size大于threshold时,进行数组扩容(扩容后数组容量依然为2的次方数)

    下图为HashMap的存储结构图,“position”,和“hello”的hash值对应的数组下标都为0,新插入的结点会存储在table数组中并通过next指针指向下一链表结点。

    HashMap根据key获取value,插入键值对,判断key是否存在和根据key删除价值对的效率很高,时间复杂度为O(1)

    因为大多数的key可以通过hash值一一对应,少部分会冲突,冲突相对较少,冲突的时候需要遍历链表

     public V get(Object key) {
            if (key == null)
                return getForNullKey();
            Entry<K,V> entry = getEntry(key);  //根据键来获取相应结点
    
            return null == entry ? null : entry.getValue();
    }
    final Entry<K,V> getEntry(Object key) {
            if (size == 0) {
                return null;
            }
    
            int hash = (key == null) ? 0 : hash(key);  //根据key计算hash值
            for (Entry<K,V> e = table[indexFor(hash, table.length)];  //根据hash值和数组长度获取结点相应的数组下标
                 e != null;
                 e = e.next) {
                Object k;
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))  //遍历链表获取根据hash值和key值找对应的结点
                    return e;
            }
            return null;
        }
    public boolean containsKey(Object key) {
            return getEntry(key) != null;
        }
     public V put(K key, V value) {
            if (table == EMPTY_TABLE) {
                inflateTable(threshold);
            }
            if (key == null)
                return putForNullKey(value);
            int hash = hash(key);
            int i = indexFor(hash, table.length);  //根据key计算hash值和数组下标
            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;    // 遍历链表,如果存在key值,则替换原来的values值
                }
            }
    
            modCount++;
            addEntry(hash, key, value, i);  //不存在则新增结点,并存入table[i]中,新增结点会存储在数组中
            return null;
        }
     final Entry<K,V> removeEntryForKey(Object key) {
            if (size == 0) {
                return null;
            }
            int hash = (key == null) ? 0 : hash(key);
            int i = indexFor(hash, table.length);
            Entry<K,V> prev = table[i];
            Entry<K,V> e = prev;
    
            while (e != null) {
                Entry<K,V> next = e.next;
                Object k;
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k)))) {
                    modCount++;
                    size--;
                    if (prev == e)
                        table[i] = next;
                    else
                        prev.next = next;  //删除节点后,将节点的后继结点存入数组或者将此结点的后继与前驱相连
                    e.recordRemoval(this);
                    return e;
                }
                prev = e;
                e = next;
            }
    
            return e;
        }

     HashMap的遍历:

    public static void main(String[] args) {
            
            HashMap<String, Integer> map=new HashMap<>();
            map.put("1111", 1);
            map.put("2222", 2);
            map.put("3333", 3);
            System.out.println(map.entrySet());
            System.out.println(map.keySet());
            Iterator<Entry<String, Integer>> it=map.entrySet().iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
                
            }
        }

    输出:

    [2222=2, 1111=1, 3333=3]

    [2222, 1111, 3333]

    2222=2

    1111=1

    3333=3

  • 相关阅读:
    历史版本xcode的下载
    mac上安装hg
    xcode不能抓帧
    window buffer alignment
    highp 和 mediump
    AFBC mali
    AO composition
    gpu memory wait
    L2 cache//bifrost --- cortex-A55
    效果样式
  • 原文地址:https://www.cnblogs.com/peng111/p/6146380.html
Copyright © 2011-2022 走看看