映射(Map)

image.png

Map的接口设计

  1. public interface CustMap<K, V> {
  2. int size();
  3. boolean isEmpty();
  4. void clear();
  5. V put(K key ,V value);
  6. V get(K key);
  7. V removet(K key);
  8. boolean containsKey(K key);
  9. boolean containsValue(V value);
  10. }

Map 与 Set

  • map 的所有key组合在一起就是set

TreeMap分析

image.png

image.png

哈希表 (Hash Table)

image.png

哈希冲突

image.png
image.png

哈希函数

image.png

如何生成Key 的哈希值

image.png
image.png

字符串的哈希函数

image.png
image.png
image.png
image.png

哈希值的进一步处理

JDK 8 的源码:

  1. static final int hash(Object key) {
  2. int h;
  3. return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
  4. }