Java Hashtable computeIfPresent() 方法



描述

Java Hashtable computeIfPresent() 方法用於計算指定鍵的對映(如果指定鍵已與某個值關聯(或對映到 null)),使用給定的對映函式並將該對映輸入到此雜湊表中,除非為 null。

宣告

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

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

引數

key − 與指定值關聯的鍵

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

返回值

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

異常

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

在整數、整數對的雜湊表中計算存在特定鍵的對映示例

以下示例演示了 Java Hashtable computeIfPresent() 方法的使用,以獲取雜湊表的更新值。我們建立了兩個整數、整數對的雜湊表物件。然後將一些條目新增到雜湊表中,然後使用 computeIfPresent() 方法更新值,然後列印更新後的雜湊表。

package com.tutorialspoint;

import java.util.Hashtable;

public class HashtableDemo {
   public static void main(String args[]) {
      
      // create two hash tables
      Hashtable<Integer, Integer> hashtable = new Hashtable<>();

      // populate hashtable
      hashtable.put(1, 1);
      hashtable.put(3, 3); 
     
      // print the hashtable
      System.out.println("Hashtable: " + hashtable);
 
      // update the values of the hashtable
      hashtable.computeIfPresent(1, (key,value) -> value * 10);
      hashtable.computeIfPresent(2, (key,value) -> value * 20);
      hashtable.computeIfPresent(3, (key,value) -> value * 30);

      System.out.println("Updated Hashtable: " + hashtable);   
   }    
}

輸出

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

Hashtable: {3=3, 1=1}
Updated Hashtable: {3=90, 1=10}

在整數、字串對的雜湊表中計算存在特定鍵的對映示例

以下示例演示了 Java Hashtable compute() 方法的使用,以獲取雜湊表的更新值。我們建立了兩個整數、字串的雜湊表物件。然後將一些條目新增到雜湊表中,然後使用 computeIfPresent() 方法更新值,然後列印更新後的雜湊表。

package com.tutorialspoint;

import java.util.Hashtable;

public class HashtableDemo {
   public static void main(String args[]) {
      
      // create two hash tables
      Hashtable<Integer, String> hashtable = new Hashtable<>();

      // populate hashtable
      hashtable.put(1, "A");
      hashtable.put(3, "C"); 
     
      // print the hashtable
      System.out.println("Hashtable: " + hashtable);
 
      // update the values of the hashtable
      hashtable.computeIfPresent(1, (key,value) -> value.concat("123"));
      hashtable.computeIfPresent(2, (key,value) -> value.concat("456"));
      hashtable.computeIfPresent(3, (key,value) -> value.concat("789"));

      System.out.println("Updated Hashtable: " + hashtable);   
   }    
}

輸出

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

Hashtable: {3=C, 1=A}
Updated Hashtable: {3=C789, 1=A123}

在整數、物件對的雜湊表中計算存在特定鍵的對映示例

以下示例演示了 Java Hashtable compute() 方法的使用,以獲取雜湊表的更新值。我們建立了兩個整數、Student 對的雜湊表物件。然後將一些條目新增到雜湊表中,然後使用 computeIfPresent() 方法更新值,然後列印更新後的雜湊表。

package com.tutorialspoint;

import java.util.Hashtable;

public class HashtableDemo {
   public static void main(String args[]) {
      
      // create two hash tables
      Hashtable<Integer, Student> hashtable = new Hashtable<>();

      // populate hashtable
      hashtable.put(1, new Student(1, "Julie"));
      hashtable.put(3, new Student(3, "Adam"));
     
      // print the hashtable
      System.out.println("Hashtable: " + hashtable);
 
      // update the values of the hashtable
      hashtable.computeIfPresent(1, (key,value) -> value.update("Roberts"));
      hashtable.computeIfPresent(2, (key,value) -> value.update("Pitts"));
      hashtable.computeIfPresent(3, (key,value) -> value.update("Cruise"));

      System.out.println("Updated Hashtable: " + hashtable);   
   }    
}
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 + " ]";
   }
}

輸出

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

Hashtable: {3=[ 3, Adam ], 1=[ 1, Julie ]}
Updated Hashtable: {3=[ 3, Adam Cruise ], 1=[ 1, Julie Roberts ]}
java_util_hashtable.htm
廣告