當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java HashMap replace(key, value)用法及代碼示例


HashMap類實現的Map接口的replace(K key,V value)方法僅在先前將鍵映射為某個值時才用於替換指定鍵的值。

用法:

public V replace(K key, V value)

參數:此方法接受兩個參數:


  • key:這是必須替換其值的元素的鍵。
  • value:這是必須與提供的鍵映射的新值。

返回值:此方法返回與指定鍵關聯的先前值。如果沒有這樣的鍵映射,則如果實現支持空值,則它返回null。

異常:該方法將拋出:

  • NullPointerException 如果指定的鍵或值是null,並且此映射不允許空鍵或值,並且
  • IllegalArgumentException如果指定鍵或值的某些屬性阻止將其存儲在此映射中。

程序1:

// 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<String, Integer> 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}

程序2:

// 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<String, Integer> 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<String, Integer> 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://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#replace-K-V-



相關用法


注:本文由純淨天空篩選整理自iamvineettiwari012大神的英文原創作品 HashMap replace(key, value) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。