zoukankan      html  css  js  c++  java
  • HashMap—— values() remove方法 containsKey()方法 containsValue()方法

    values()方法:看下面的实例,就是把所有的value值封装成一个connection型的数组

            Map<Integer,Student> students=new HashMap<>();
            students.put(1, new Student("111"));
            students.put(2, new Student("111"));
            Collection stuValues = students.values();
            for (Object stuValue : stuValues) {
                System.out.println(stuValue);
            }

    输出结果

    com.xt.map.Student@52e922
    com.xt.map.Student@25154f

    remove()方法

     final Node<K,V> removeNode(int hash, Object key, Object value,
                                   boolean matchValue, boolean movable) {
            Node<K,V>[] tab; Node<K,V> p; int n, index;
            if ((tab = table) != null && (n = tab.length) > 0 &&
                (p = tab[index = (n - 1) & hash]) != null) {
                Node<K,V> node = null, e; K k; V v;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    node = p;
                else if ((e = p.next) != null) {
                    if (p instanceof TreeNode)
                        node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
                    else {
                        do {
                            if (e.hash == hash &&
                                ((k = e.key) == key ||
                                 (key != null && key.equals(k)))) {
                                node = e;
                                break;
                            }
                            p = e;
                        } while ((e = e.next) != null);
                    }
                }
                if (node != null && (!matchValue || (v = node.value) == value ||
                                     (value != null && value.equals(v)))) {
                    if (node instanceof TreeNode)
                        ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
                    else if (node == p)
                        tab[index] = node.next;
                    else
                        p.next = node.next;
                    ++modCount;
                    --size;
                    afterNodeRemoval(node);
                    return node;
                }
            }
            return null;
        }

    看上面的红色代码,这个事底层代码,就是判定remove的是谁,,,怎么去判断的依据,下面就是依据

        1:判定HashCode值是否相同       e.hash == hash

        2:判定地址是否相同  k = e.key) == key

        3:equals方法  key.equals(k))

                     if (e.hash == hash &&
                                ((k = e.key) == key ||
                                 (key != null && key.equals(k)))) {

     如果为真就删除了这个对象

    containsKey()方法

     */
        final Node<K,V> getNode(int hash, Object key) {
            Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
            if ((tab = table) != null && (n = tab.length) > 0 &&
                (first = tab[(n - 1) & hash]) != null) {
                if (first.hash == hash && // always check first node
                    ((k = first.key) == key || (key != null && key.equals(k))))
                    return first;
                if ((e = first.next) != null) {
                    if (first instanceof TreeNode)
                        return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                    do {
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            return e;
                    } while ((e = e.next) != null);
                }
            }
            return null;
        }

    我们发现红色的字体和remove方法道理相同,这里就不过多解释了

    containsValues()方法

    public boolean containsValue(Object value) {
            Node<K,V>[] tab; V v;
            if ((tab = table) != null && size > 0) {
                for (int i = 0; i < tab.length; ++i) {
                    for (Node<K,V> e = tab[i]; e != null; e = e.next) {
                        if ((v = e.value) == value ||
                            (value != null && value.equals(v)))
                            return true;
                    }
                }
            }
            return false;
        }
    (v = e.value) == value || (value != null && value.equals(v))
    这个意思就是:如果value地址相同,或者本身的值相同,然后equals方法
    如果是基本类型:例如2==2 直接就为TRUE
    如果是引用类型:new Student(111)==new Student(222) 为FALSE 但是value.equals(v)为真 最后还是返回TRUE

    OK~
  • 相关阅读:
    hdu 5387 Clock (模拟)
    CodeForces 300B Coach (并查集)
    hdu 3342 Legal or Not(拓扑排序)
    hdu 3853 LOOPS(概率DP)
    hdu 3076 ssworld VS DDD(概率dp)
    csu 1120 病毒(LICS 最长公共上升子序列)
    csu 1110 RMQ with Shifts (线段树单点更新)
    poj 1458 Common Subsequence(最大公共子序列)
    poj 2456 Aggressive cows (二分)
    HDU 1869 六度分离(floyd)
  • 原文地址:https://www.cnblogs.com/lyxcode/p/9467034.html
Copyright © 2011-2022 走看看