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
广告

© . All rights reserved.