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

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

发布时间:2022-11-23 12:38:05 所属栏目:语言 来源:
导读:  高手过招,招招致命
  JDK1.8 中 HashMap 的底层实现,我相信大家都能说上来个 一二,底层数据结构 数组 + 链表(或红黑树) ,源码如下:
  
  /**
   * 数组
   */
  transient
  高手过招,招招致命
  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;    
      ...   
 

(编辑:云计算网_汕头站长网)

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