Map 学习

interface Map :implements 有 HashMap,TreeMap;
Map的主要方法:
put();
putAll();
get(key);
remove(key);
containsKey(key);
containsValue(value);
ketSet();
values()(Collection);
entrySet()(key=value…);
isEmpty();


HashMap:
[/color
变量:

// table 里存放 该 Map中全部数据;
[color=darkred]transient Entry[] table;

/////////////////////////////////////////////////////////////////////
Entry<K,V>定义:static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        final int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }
/////////////////////////////////////////////////////////////////////
     * The number of times this HashMap has been structurally modified
     * Structural modifications are those that change the number of mappings in
     * the HashMap or otherwise modify its internal structure (e.g.,
     * rehash).  This field is used to make iterators on Collection-views of
     * the HashMap fail-fast.  (See ConcurrentModificationException).
     */
    transient int modCount;
put 方法源代码:

public V put(K key, V value) {
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }

void addEntry(int hash, K key, V value, int bucketIndex) {        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
        if (size++ >= threshold)
            resize(2 * table.length);
    }

---------------------------------------------------------------------
---------------------------------------------------------------------
由此可见:
HashMap特点:
1.数据存放在Entry 数组中(table)
2.按照自定义key的hash值来存储。
3.初始化一个 数组,数组的 类型是 Entry的。
4.同一hash 的不同key,存放在 table数组同一 下表处。。。组成一个小链表。
5.put时 如果存在同一key值,则只修改对应的value值。
6.只能存在一个null key。


LinkedHashMap


类定义:
---------------------------------------------------------------------
public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>
{ private transient Entry<K,V> header;.....
}
---------------------------------------------------------------------
private static class Entry<K,V> extends HashMap.Entry<K,V> {
        // These fields comprise the doubly linked list used for iteration.
        Entry<K,V> before, after;
---------------------------------------------------------------------
由此可见LinkedHashMap所有的成员变量如下:

transient Entry[] table;
private transient Entry<K,V> header;

Entry<K,V> 变量如下:
        final K key;
        V value;
        Entry<K,V> next;
        final int hash;
        Entry<K,V> before, after;
所以:LinkedHashMap用 双向链表即实现了Map又实现了链表。。解决了HashMap无序的问题。 但是,其 空间利用率 及效率大大降低。。。

---------------------------------------------------------------------

TreeMap

主要变量:
  private final Comparator<? super K> comparator;
  private transient Entry<K,V> root = null;

static final class Entry<K,V> implements Map.Entry<K,V> {
        K key;
        V value;
        Entry<K,V> left = null;
        Entry<K,V> right = null;
        Entry<K,V> parent;
        boolean color = BLACK;
排序是按照 Comparator来判断,如果该变量为null,自然排序。否则,按照Comparator来排序...
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.

你可能感兴趣的:(map)