Data Structure - Map Interface

Brief introduction

One usually the most common data structures, is the key constituting the internal, Map set can not contain duplicate keys, but may contain duplicate values.

Map Interface
public interface Map<K,V>
Map method is not implemented
// 获取元素个数
int size();
// 是否为空
boolean isEmpty();
// 是否包含key
boolean containsKey(Object key);
// 是否包含value
boolean containsValue(Object value);
// 根据key取值
V get(Object key);
// 添加元素
V put(K key, V value);
// 移除key
V remove(Object key);
// 添加一个Map集合
void putAll(Map<? extends K, ? extends V> m);
// 清除
void clear();
// 获取整个key集合
Set<K> keySet();
// 获取所有值
Collection<V> values();
// 获取所有键值对
Set<Map.Entry<K, V>> entrySet();
// 比较是否一样
boolean equals(Object o);
// 获取集合hashCode
int hashCode();
Map the default method
default V getOrDefault(Object key, V defaultValue) {
    V v;
    // key不存在或key对应值为空返回默认值,否有返回实际值
    return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
}

The value acquisition key, the default value is returned empty or absent

default V putIfAbsent(K key, V value) {
    V v = get(key);
    // key为空则添加
    if (v == null) {
        v = put(key, value);
    }
    return v;
}

If the key does not exist or is empty then add this key-value pairs

default boolean remove(Object key, Object value) {
    Object curValue = get(key);
    // value不一样或key不存在返回false
    if (!Objects.equals(curValue, value) ||
            (curValue == null && !containsKey(key))) {
        return false;
    }
    remove(key);
    return true;
}

If the key value corresponding to the same value to delete

default boolean replace(K key, V oldValue, V newValue) {
    Object curValue = get(key);
    // 旧值与key对应值不同时返回false
    if (!Objects.equals(curValue, oldValue) ||
            (curValue == null && !containsKey(key))) {
        return false;
    }
    put(key, newValue);
    return true;
}

Replace the old value with the new value, and old value does not return a value corresponding to the key at the same time false

default V replace(K key, V value) {
    V curValue;
    if (((curValue = get(key)) != null) || containsKey(key)) {
        curValue = put(key, value);
    }
    return curValue;
}

When the key is present, a value covering the original value, return an empty does not exist

Map internal class Entry Interface
interface Entry<K,V>
Map Entry inner class method
K getKey();
V getValue();
V setValue(V value);
boolean equals(Object o);
int hashCode();

It defines the basic behavior of key-value pairs

AbstractMap abstract class
public abstract class AbstractMap<K,V> implements Map<K,V>

Given all the basic skeleton Map collection

AbstractMap property
// 键集合
transient Set<K>        keySet;
// 值集合
transient Collection<V> values;
AbstractMap Constructor

protected AbstractMap() {
}

AbstractMap method is not implemented
public abstract Set<Entry<K,V>> entrySet();

AbstractMap implements a method except the entrySet () other than the Map interface.

The method has been implemented AbstractMap

In these methods have been implemented, only need to focus equals and hashcode methods can be, other methods have been covered by subclass

public boolean equals(Object o) {
    // 地址一样时为true
    if (o == this)
        return true;
    // 不是Map或子类实例时为false
    if (!(o instanceof Map))
        return false;
    // 强转o为Map
    Map<?,?> m = (Map<?,?>) o;
    // m长度为空
    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();
            // 有任意一个值不像等就返回false
            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;
    }
    // 循环完返回true
    return true;
}

Analyzing equals manner, to determine the type, length is determined, after the determination of elements, which is different from List, List of elements in the sequence will lead to different equals different, here comprising using a comparison mode and value. The first element is either empty, other elements can no order limit.

public int hashCode() {
    int h = 0;
    // 遍历所有元素
    Iterator<Entry<K,V>> i = entrySet().iterator();
    while (i.hasNext())
        // 元素hashCode累加
        h += i.next().hashCode();
    return h;
}

hashcode method is to sum all the elements hashcode

Guess you like

Origin www.cnblogs.com/yuanjiangnan/p/12606960.html