加入收藏 | 设为首页 | 会员中心 | 我要投稿 济南站长网 (https://www.0531zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

HashMap面试常见的六连问,你可以扛得住吗?

发布时间:2021-12-06 19:35:45 所属栏目:语言 来源:互联网
导读:高手过招,招招致命 JDK1.8 中 HashMap 的底层实现,我相信大家都能说上来个 一二,底层数据结构 数组 + 链表(或红黑树) ,源码如下: /** * 数组 */ transient NodeK,V[] table; /** * 链表结构 */ static class NodeK,V implements Map.EntryK,V { fin
HashMap面试常见的六连问,你可以扛得住吗?
高手过招,招招致命
JDK1.8 中 HashMap 的底层实现,我相信大家都能说上来个 一二,底层数据结构 数组 + 链表(或红黑树) ,源码如下:
 
/**    
 * 数组    
 */    
transient Node<K,V>[] table;  
 /**   
 * 链表结构    
 */    
static class Node<K,V> implements Map.Entry<K,V> {    
    final int hash;    
    final K key;    
    V value;    
    Node<K,V> next;    
    Node(int hash, K key, V value, Node<K,V> next) {    
        this.hash = hash;    
        this.key = key;    
        this.value = value;    
        this.next = next;    
    }    
    public final K getKey()        { return key; }
    public final V getValue()      { return value; }    
    public final String toString() { return key + "=" + value; }    
    public final int hashCode() {    
        return Objects.hashCode(key) ^ Objects.hashCode(value);   
     }    
    public final V setValue(V newValue) {    
        V oldValue = value;    
        value = newValue;    
        return oldValue;    
    }    
    public final boolean equals(Object o) {    
        if (o == this)    
            return true;    
        if (o instanceof Map.Entry) {   
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;    
            if (Objects.equals(key, e.getKey()) &&    
                Objects.equals(value, e.getValue()))   
                return true;   
        }    
        return false;    
    }    
}    
**    
 * 红黑树结构    
 */    
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {    
    TreeNode<K,V> parent;  // red-black tree links   
    TreeNode<K,V> left;    
    TreeNode<K,V> right;    
    TreeNode<K,V> prev;    // needed to unlink next upon deletion    
    boolean red;    
    ...   

(编辑:济南站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读