Java HashMap compute() 方法



描述

Java HashMap compute() 方法用於計算指定鍵及其當前對映值(如果當前沒有對映則為 null)的對映。例如,要建立或追加一個字串 msg 到值對映。

宣告

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

public V compute​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)

引數

key − 與指定值關聯的鍵

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

返回值

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

異常

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

整數、整數對 HashMap 的對映計算示例

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

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(2, 2);
      newmap.put(3, 3); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.compute(1, (key,value) -> value * 10);
      newmap.compute(2, (key,value) -> value * 20);
      newmap.compute(3, (key,value) -> value * 30);

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

輸出

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

Map: {1=1, 2=2, 3=3}
Updated Map: {1=10, 2=40, 3=90}

整數、字串對 HashMap 的對映計算示例

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

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(2, "B");
      newmap.put(3, "C"); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.compute(1, (key,value) -> value.concat("123"));
      newmap.compute(2, (key,value) -> value.concat("456"));
      newmap.compute(3, (key,value) -> value.concat("789"));

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

輸出

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

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

整數、學生對 HashMap 的對映計算示例

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

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(2, new Student(2, "Robert"));
      newmap.put(3, new Student(3, "Adam"));
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.compute(1, (key,value) -> value.update("Roberts"));
      newmap.compute(2, (key,value) -> value.update("Pitts"));
      newmap.compute(3, (key,value) -> value.update("Cruise"));

      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 ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
Updated Map: {1=[ 1, Julie Roberts ], 2=[ 2, Robert Pitts ], 3=[ 3, Adam Cruise ]}
java_util_hashmap.htm
廣告

© . All rights reserved.