AbstractMap

类关系

1
2
//实现了Map接口
public abstract class AbstractMap<K,V> implements Map<K,V>
  • Map接口

构造器

方法

  • 判断是否包含键或值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    //使用迭代器判断是否含有值
    public boolean containsValue(Object value) {
    Iterator<Entry<K,V>> i = entrySet().iterator();
    if (value==null) {
    while (i.hasNext()) {
    Entry<K,V> e = i.next();
    if (e.getValue()==null)
    return true;
    }
    } else {
    while (i.hasNext()) {
    Entry<K,V> e = i.next();
    if (value.equals(e.getValue()))
    return true;
    }
    }
    return false;
    }

    //使用迭代器判断是否含有键
    public boolean containsKey(Object key) {
    Iterator<Map.Entry<K,V>> i = entrySet().iterator();
    if (key==null) {
    while (i.hasNext()) {
    Entry<K,V> e = i.next();
    if (e.getKey()==null)
    return true;
    }
    } else {
    while (i.hasNext()) {
    Entry<K,V> e = i.next();
    if (key.equals(e.getKey()))
    return true;
    }
    }
    return false;
    }
  • 增删改查

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    //由键获取值
    public V get(Object key) {
    Iterator<Entry<K,V>> i = entrySet().iterator();
    if (key==null) {
    while (i.hasNext()) {
    Entry<K,V> e = i.next();
    if (e.getKey()==null)
    return e.getValue();
    }
    } else {
    while (i.hasNext()) {
    Entry<K,V> e = i.next();
    if (key.equals(e.getKey()))
    return e.getValue();
    }
    }
    return null;
    }

    //不支持添加元素,会抛出异常,想要增加元素需要子类重写该方法
    public V put(K key, V value) {
    throw new UnsupportedOperationException();
    }

    //根据所给键删除元素
    public V remove(Object key) {
    Iterator<Entry<K,V>> i = entrySet().iterator();
    Entry<K,V> correctEntry = null;
    if (key==null) {
    while (correctEntry==null && i.hasNext()) {
    Entry<K,V> e = i.next();
    if (e.getKey()==null)
    correctEntry = e;
    }
    } else {
    while (correctEntry==null && i.hasNext()) {
    Entry<K,V> e = i.next();
    if (key.equals(e.getKey()))
    correctEntry = e;
    }
    }

    V oldValue = null;
    if (correctEntry !=null) {
    oldValue = correctEntry.getValue();
    i.remove();
    }
    return oldValue;
    }
  • 返回键或值的集合

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    //临时变量
    transient Set<K> keySet; //将Map中的键放在Set中
    transient Collection<V> values; //将Map中的值放在Collection中

    //得到键的集合
    public Set<K> keySet() {
    Set<K> ks = keySet;
    if (ks == null) {
    ks = new AbstractSet<K>() {
    public Iterator<K> iterator() {
    return new Iterator<K>() {
    private Iterator<Entry<K,V>> i = entrySet().iterator();

    public boolean hasNext() {
    return i.hasNext();
    }

    public K next() {
    return i.next().getKey();
    }

    public void remove() {
    i.remove();
    }
    };
    }

    public int size() {
    return AbstractMap.this.size();
    }

    public boolean isEmpty() {
    return AbstractMap.this.isEmpty();
    }

    public void clear() {
    AbstractMap.this.clear();
    }

    public boolean contains(Object k) {
    return AbstractMap.this.containsKey(k);
    }
    };
    keySet = ks;
    }
    return ks;
    }

    //得到值的集合
    public Collection<V> values() {
    Collection<V> vals = values;
    if (vals == null) {
    vals = new AbstractCollection<V>() {
    public Iterator<V> iterator() {
    return new Iterator<V>() {
    private Iterator<Entry<K,V>> i = entrySet().iterator();

    public boolean hasNext() {
    return i.hasNext();
    }

    public V next() {
    return i.next().getValue();
    }

    public void remove() {
    i.remove();
    }
    };
    }

    public int size() {
    return AbstractMap.this.size();
    }

    public boolean isEmpty() {
    return AbstractMap.this.isEmpty();
    }

    public void clear() {
    AbstractMap.this.clear();
    }

    public boolean contains(Object v) {
    return AbstractMap.this.containsValue(v);
    }
    };
    values = vals;
    }
    return vals;
    }
  • 重写equals方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    public boolean equals(Object o) {
    if (o == this)
    return true;

    if (!(o instanceof Map))
    return false;
    Map<?,?> m = (Map<?,?>) o;
    if (m.size() != size())
    return false;

    try {
    Iterator<Entry<K,V>> i = entrySet().iterator();
    while (i.hasNext()) {
    Entry<K,V> e = i.next();
    K key = e.getKey();
    V value = e.getValue();
    if (value == null) {
    if (!(m.get(key)==null && m.containsKey(key)))
    return false;
    } else {
    if (!value.equals(m.get(key)))
    return false;
    }
    }
    } catch (ClassCastException unused) {
    return false;
    } catch (NullPointerException unused) {
    return false;
    }

    return true;
    }

HashMap

类关系图

构造器

成员变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//HashMap的默认初始容量
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

//HashMap的最大容量
static final int MAXIMUM_CAPACITY = 1 << 30; //aka 2**30

//负载因子,代表了table的填充度有多少,默认是0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;

//将链表转化为红黑树的阈值
static final int TREEIFY_THRESHOLD = 8;
//将红黑树转化为链表的阈值
static final int UNTREEIFY_THRESHOLD = 6;

//树的最小容量?
static final int MIN_TREEIFY_CAPACITY = 64;

//底层是Node数组加链表或红黑树
transient Node<K,V>[] table;

transient Set<Map.Entry<K,V>> entrySet;

//包含键值对的个数
transient int size;

//结构性修改HashMap的次数
transient int modCount;

//扩容阈值,大于该值且位置不为空,则进行resize()。
int threshold;

final float loadFactor;

内部类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}
}

方法

  • 哈希算法
    1
    2
    3
    4
    5
    //计算键的插入位置 
    static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
  • 扩容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //通过位运算返回大于cap且与cap最近的2的整数倍
    static final int tableSizeFor(int cap) {
    int n = cap - 1;
    n |= n >>> 1;
    n |= n >>> 2;
    n |= n >>> 4;
    n |= n >>> 8;
    n |= n >>> 16;
    return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }