有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java映射什么。放回?

我试图获取map接口的put方法的返回类型。当我第一次打印时,它打印空值,在更新密钥后,我得到上一个值。 那么,有人能告诉我map接口中put方法的返回类型是什么吗

Map<Integer, String> map = new HashMap<Integer, String>();
System.out.println(map.put(1, "ABC"));
System.out.println(map.put(1, "XYZ"));

Output:
null
ABC

共 (2) 个答案

  1. # 1 楼答案

    根据java文档:

    The put returns the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with the key if the implementation supports null values.)

    在你的例子中,当你使用map.put(1, "ABC")时,没有任何东西与key 1相关,所以它returns null但是当你使用put(1, "XYZ")时,那么已经有一个entry existskey 1相对,所以它返回“ABC”

  2. # 2 楼答案

    你可以在文件中阅读:here

    V put(K key, V value)
    

    Returns: the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key, if the implementation supports null values.)

    既然你的地图被宣布为

    Map<Integer, String> map
    

    V是字符串类型,因此调用put将返回字符串

    但是我得到的价值是什么<也许你在问自己

    然后深入查看源代码

    HashMap的put方法实际上是调用最后一个方法putVal

    其实现方式如下:

    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
            Node<K,V>[] tab; Node<K,V> p; int n, i;
            if ((tab = table) == null || (n = tab.length) == 0)
                n = (tab = resize()).length;
            if ((p = tab[i = (n - 1) & hash]) == null)
                tab[i] = newNode(hash, key, value, null);
            else {
                Node<K,V> e; K k;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;
                else if (p instanceof TreeNode)
                    e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
                else {
                    for (int binCount = 0; ; ++binCount) {
                        if ((e = p.next) == null) {
                            p.next = newNode(hash, key, value, null);
                            if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                                treeifyBin(tab, hash);
                            break;
                        }
                        if (e.hash == hash &&
                            ((k = e.key) == key || (key != null && key.equals(k))))
                            break;
                        p = e;
                    }
                }
                if (e != null) { // existing mapping for key
                    V oldValue = e.value;
                    if (!onlyIfAbsent || oldValue == null)
                        e.value = value;
                    afterNodeAccess(e);
                    return oldValue;
                }
            }
            ++modCount;
            if (++size > threshold)
                resize();
            afterNodeInsertion(evict);
            return null;
        }
    

    在逻辑中,方法返回给定键在映射中找到的前一个值

    这意味着你第一次这么做

    put(1,"abc"); 它返回null,因为地图中没有任何内容。 如果你再这样做 put(1,"xyz");然后该方法返回“abc”,因为这是在映射中找到的键=1的最后一个值