📜  Java中的 HashMap putIfAbsent(key, value) 方法及示例

📅  最后修改于: 2022-05-13 01:55:29.776000             🧑  作者: Mango

Java中的 HashMap putIfAbsent(key, value) 方法及示例

HashMapputIfAbsent(K key, V value)方法用于将指定的键与指定的值进行映射,仅当此 HashMap 实例中不存在(或映射为空)该键时。

句法:

public V putIfAbsent(K key, V value)

参数:此方法接受两个参数:

  • 键:这是必须映射提供的值的键。
  • value:这是必须与提供的键相关联的值,如果不存在的话。

返回值:

此方法返回null (如果之前没有与提供的键的映射,或者它已映射到空值)或与提供的键关联的当前值

例外:

此方法可能会抛出以下异常:

  • NullPointerException:如果指定的键或值为空,并且此映射不支持空值。
  • IllegalArgumentException:如果指定的键或值阻止它存储在映射中。
  • UnsupportedOperationException – 如果此映射不支持 put 操作(可选)
  • ClassCastException – 如果键或值的类型不适合此映射(可选)

方案一:

Java
// Java program to demonstrate
// putIfAbsent(Key, value) method.
 
import java.util.HashMap;
 
public class TestClass {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create a HashMap and add some values
        HashMap map = new HashMap<>();
        map.put("a", 10000);
        map.put("b", 55000);
        map.put("c", 44300);
        map.put("e", 53200);
 
        // print original map
        System.out.println("HashMap:\n " + map.toString());
 
        // put a new value which is not mapped
        // before in map
        map.putIfAbsent("d", 77633);
 
        System.out.println("New HashMap:\n " + map);
 
        // try to put a new value with existing key
        // before in map
        map.putIfAbsent("d", 55555);
 
        // print newly mapped map
        System.out.println("Unchanged HashMap:\n " + map);
    }
}


Java
// Java program to demonstrate
// putIfAbsent(Key, value) method.
 
import java.util.*;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create a HashMap and add some values
        HashMap map = new HashMap<>();
        map.put("a", 10000);
        map.put("b", 55000);
        map.put("c", 44300);
        map.put("e", null);
 
        // print original map
        System.out.println("HashMap:\n " + map.toString());
 
        // put a new value which is not mapped
        // before in map and store the returned
        // value in r1
        Integer r1 = map.putIfAbsent("d", 77633);
 
        // put a new value for key 'e' which is mapped
        // with a null value, and store the returned
        // value in r2
        Integer r2 = map.putIfAbsent("e", 77633);
 
        // print the value of r1
        System.out.println("Value of r1:\n " + r1);
 
        // print the value of r2
        System.out.println("Value of r2:\n " + r2);
 
        // print newly mapped map
        System.out.println("New HashMap:\n " + map);
    }
}


输出
HashMap:
 {a=10000, b=55000, c=44300, e=53200}
New HashMap:
 {a=10000, b=55000, c=44300, d=77633, e=53200}
Unchanged HashMap:
 {a=10000, b=55000, c=44300, d=77633, e=53200}

方案二:

Java

// Java program to demonstrate
// putIfAbsent(Key, value) method.
 
import java.util.*;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create a HashMap and add some values
        HashMap map = new HashMap<>();
        map.put("a", 10000);
        map.put("b", 55000);
        map.put("c", 44300);
        map.put("e", null);
 
        // print original map
        System.out.println("HashMap:\n " + map.toString());
 
        // put a new value which is not mapped
        // before in map and store the returned
        // value in r1
        Integer r1 = map.putIfAbsent("d", 77633);
 
        // put a new value for key 'e' which is mapped
        // with a null value, and store the returned
        // value in r2
        Integer r2 = map.putIfAbsent("e", 77633);
 
        // print the value of r1
        System.out.println("Value of r1:\n " + r1);
 
        // print the value of r2
        System.out.println("Value of r2:\n " + r2);
 
        // print newly mapped map
        System.out.println("New HashMap:\n " + map);
    }
}
输出
HashMap:
 {a=10000, b=55000, c=44300, e=null}
Value of r1:
 null
Value of r2:
 null
New HashMap:
 {a=10000, b=55000, c=44300, d=77633, e=77633}

参考: https: Java/util/HashMap.html#putIfAbsent-KV-