HashMap面试总结

91 阅读3分钟
原文链接: www.jianshu.com

本着针对面试,不负责任的态度,写下《面试总结》系列。本系列记录面试过程中各个知识点,而不是入门系列,如果有不懂的自行学习。

不负责任系列

  • 基础知识

    1. 可以接受null键和值,而Hashtable则不能
    2. 非synchronized,所以很快
    3. 存储的是键值对
    4. 使用数组+链表的方式存储数据
    5. 对key进行hash(散列)算法,所以顺序不固定
    6. 实际使用Node存储
  • 常量&变量


 // public class HashMap extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {}
  /**
   * The default initial capacity - MUST be a power of two.
   默认数组长度
   */
  static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; 

  /**
   * The maximum capacity, used if a higher value is implicitly specified
   * by either of the constructors with arguments.
   * MUST be a power of two <= 1<<30.
   * 数组最大长度
   */
  static final int MAXIMUM_CAPACITY = 1 << 30;

  /**
   * The load factor used when none specified in constructor.
   * 默认装填因子
   */
  static final float DEFAULT_LOAD_FACTOR = 0.75f;
  
  
  static class Node<K,V> implements Map.Entry<K,V> {
      final int hash;
      final K key;
      V value;
      Node<K,V> next;
  }
  
    /**
   * The number of key-value mappings contained in this map.
   */
  transient int size;

  /**
   * 阈值
   * The next size value at which to resize (capacity * load factor).
   *
   * @serial
   */
  // (The javadoc description is true upon serialization.
  // Additionally, if the table array has not been allocated, this
  // field holds the initial array capacity, or zero signifying
  // DEFAULT_INITIAL_CAPACITY.)
  int threshold;
  
  public V put(K key, V value) {
      return putVal(hash(key), key, value, false, true);
  }
  //实际存储方法
  final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {}
  //扩容方法
  final Node<K,V>[] resize() {}
  
  
  public V get(Object key) {
      Node<K,V> e;
      return (e = getNode(hash(key), key)) == null ? null : e.value;
  }
  //实际取值方法
  final Node<K,V> getNode(int hash, Object key) {}
  • 用法
    1. put(key,value) 调用hashCode(key),使用node存储hash,key,value,如果hashcode存在则使用链表存储。

    2. get(key) 根据key的hashcode找到Entry,然后获取值对象,如果根据hashcode找到的是个链表,再去根据key.equals()判断,链表中正确的节点。

  • 关于扩容

    当HashMap的大小超过了阈值(size> threshold)的时候(默认的装填因子为0.75,也就是说当一个map填满了它定义容量的75%就会去扩容),HashMap大小会扩大到原来的2倍。整个过程类似于创建新的数组,将原数组的元素重新hash后放到新数组中(rehashing)。


HashMap是非同步的,所以在多线程中使用时需要注意扩容等问题


  • 相关概念
    • hashing的概念
    • HashMap中解决碰撞的方法
    • equals()和hashCode()的应用,以及它们在HashMap中的重要性
    • 不可变对象的好处
    • HashMap多线程的条件竞争
    • 重新调整HashMap的大小

参考地址:www.importnew.com/7099.html

以上是网上能搜到的解释,下面是个人总结的知识点提要


如面试遇到此问题,第一步,反问面试官,您说的是哪个版本的HashMap

  • hashmap底层使用 数组+链表 的数据结构,实现存储数据,使用拉链法解决碰撞问题。
  • map.put(key,value)的时候,内部会对key进行一次hash算法,得到一个hash值,对这个hash值&操作得到元素在数组中的位置。
  • 如果该位置没有元素,那么直接加入,如果发生碰撞了,那么用拉链法,需要遍历链表比较key和hash值,如果有就覆盖,没有就到表尾了,所以会插到表尾。
  • 初始容量为16,加载因子0.75,当map添加的元素超过12个的时候会触发扩容机制。数组的容量翻倍,已经存入的元素做rehash的操作,重新在数组中找位置存储。
  • java8后改为碰撞链表元素超过8个,用红黑树实现
  • java8在表尾,java7是在链表头插入

思考点: 什么情况下考虑使用SparseArray和ArrayMap替换HashMap的情况