Java HashMap computeIfAbsent() 方法



描述

Java HashMap computeIfAbsent() 方法用於計算指定鍵的對映(如果指定的鍵尚未與值關聯(或對映到 null)),使用給定的對映函式並將它放入此對映中,除非為 null。

宣告

以下是java.util.HashMap.computeIfAbsent​(K key, Function<? super K,​? extends V> mappingFunction) 方法的宣告。

public V computeIfAbsent​(K key, Function<? super K,​? extends V> mappingFunction)

引數

key − 與指定值關聯的鍵

remappingFunction − 用於計算值的重新對映函式

返回值

方法呼叫返回與指定鍵關聯的新值,如果沒有則返回 null。

異常

ConcurrentModificationException − 如果檢測到重新對映函式修改了此對映。

HashMap 的 Integer, Integer 對的未對映鍵的計算對映示例

以下示例演示了 Java HashMap computeIfAbsent() 方法的使用,以獲取 Map 的更新值。我們建立了兩個 Integer,Integer 對的 Map 物件。然後向對映中添加了一些條目,然後使用 computeIfAbsent 方法更新值,最後列印更新後的對映。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Integer> newmap = new HashMap<>();

      // populate map
      newmap.put(1, 1);
      newmap.put(3, 3); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfAbsent(2, (key) -> key);      

      System.out.println("Updated Map: " + newmap);   
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Map: {1=1, 3=3}
Updated Map: {1=1, 2=2, 3=3}

HashMap 的 Integer, String 對的未對映鍵的計算對映示例

以下示例演示了 Java HashMap computeIfAbsent() 方法的使用,以獲取 Map 的更新值。我們建立了兩個 Integer,String 對的 Map 物件。然後向對映中添加了一些條目,然後使用 computeIfAbsent() 方法更新值,最後列印更新後的對映。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, String> newmap = new HashMap<>();

      // populate map
      newmap.put(1, "A");
      newmap.put(3, "C"); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfAbsent(2, (key) -> {return "B";});

      System.out.println("Updated Map: " + newmap);   
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Map: {1=A, 3=C}
Updated Map: {1=A, 2=B, 3=C}

HashMap 的 Integer, Student 對的未對映鍵的計算對映示例

以下示例演示了 Java HashMap computeIfAbsent() 方法的使用,以獲取 Map 的更新值。我們建立了兩個 Integer,Student 對的 Map 物件。然後向對映中添加了一些條目,然後使用 computeIfAbsent() 方法更新值,最後列印更新後的對映。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Student> newmap = new HashMap<>();

      // populate map
      newmap.put(1, new Student(1, "Julie"));      
      newmap.put(3, new Student(3, "Adam"));
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfAbsent(2, (key) -> {return new Student(2, "Robert");});
      
      System.out.println("Updated Map: " + newmap);   
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   
   public Student update(String surname) {
	   this.name = this.name.concat(" " + surname);
	   return this;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Map: {1=[ 1, Julie ], 3=[ 3, Adam ]}
Updated Map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
java_util_hashmap.htm
廣告
© . All rights reserved.