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

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

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

Map 接口replace(K key, V value)方法,由HashMap 类实现,用于替换指定键的值,前提是该键之前已映射了某个值。

句法:

public V replace(K key, V value)

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

  • key:这是必须替换其值的元素的
  • value:这是必须与提供的键映射的新值

返回值:此方法返回与指定键关联的先前值。如果没有这样的键映射,则返回null ,如果实现支持 null 值。

例外:此方法将抛出:

  • NullPointerException如果指定的键或值为空,并且此映射不允许空键或值,并且
  • 如果指定键或值的某些属性阻止将其存储在此映射中,则IllegalArgumentException

方案一:

// Java program to demonstrate
// replace(K key, V 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", 100);
        map.put("b", 300);
        map.put("c", 300);
        map.put("d", 400);
  
        // print map details
        System.out.println("HashMap: "
                           + map.toString());
  
        // provide value for the key which has
        // to replace it's current value,
        // using replace(K key, V value) method
        map.replace("b", 200);
  
        // print new mapping
        System.out.println("New HashMap: "
                           + map.toString());
    }
}
输出:
HashMap: {a=100, b=300, c=300, d=400}
New HashMap: {a=100, b=200, c=300, d=400}

方案二:

// Java program to demonstrate
// replace(K key, V 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", 100);
        map.put("b", 300);
        map.put("c", 300);
        map.put("d", 400);
  
        // print map details
        System.out.println("HashMap: "
                           + map.toString());
  
        // provide value for the key which has
        // to replace it's current value, and will
        // store the value in k using the
        // replace(K key, V value) method
        int k = map.replace("b", 200);
  
        // print the value of k
        System.out.println("Previous value of 'b': "
                           + k);
  
        // print new mapping
        System.out.println("New HashMap: "
                           + map.toString());
    }
}
输出:
HashMap: {a=100, b=300, c=300, d=400}
Previous value of 'b': 300
New HashMap: {a=100, b=200, c=300, d=400}

方案 3:

// Java program to demonstrate
// replace(K key, V 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", 100);
        map.put("b", 300);
        map.put("c", 300);
        map.put("d", 400);
  
        // print map details
        System.out.println("HashMap: "
                           + map.toString());
  
        // provide value for the key which is
        // not mapped previously and store the
        // return value in Integer k, using
        // replace(K key, V value) method
        Integer k = map.replace("e", 200);
  
        // print the value of k
        System.out.println("Value of k, returned "
                           + "for key 'e': " + k);
  
        // print new mapping
        System.out.println("New HashMap: "
                           + map.toString());
    }
}
输出:
HashMap: {a=100, b=300, c=300, d=400}
Value of k, returned for key 'e': null
New HashMap: {a=100, b=300, c=300, d=400}

参考资料: https: Java/util/HashMap.html#replace-KV-