站长网 语言 HashMap面试常见的六连问,你可以扛得住吗?

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

高手过招,招招致命 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;    
    …   

本文来自网络,不代表站长网立场,转载请注明出处:https://www.tzzz.com.cn/html/biancheng/yuyan/2021/1206/33855.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部