Java Hashtable computeIfAbsent() 方法



描述

Java Hashtable computeIfAbsent() 方法用于计算指定键的映射(如果指定键尚未与值关联(或映射到 null)),使用给定的映射函数并将其输入到此哈希表中,除非为 null。

声明

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

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

参数

key − 与指定值关联的键

remappingFunction − 用于计算值的重新映射函数

返回值

方法调用返回与指定键关联的新值,如果不存在则返回 null。

异常

ConcurrentModificationException − 如果检测到重新映射函数修改了此哈希表。

在整数、整数对的哈希表中为特定键计算缺失映射的示例

以下示例演示了 Java Hashtable computeIfAbsent() 方法的使用,以获取哈希表的更新值。我们创建了两个整数、整数对的哈希表对象。然后向哈希表中添加一些条目,然后使用 computeIfAbsent 方法更新值,然后打印更新后的哈希表。

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.computeIfAbsent(2, (key) -> key);      

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

输出

让我们编译并运行上述程序,这将产生以下结果。

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

在整数、字符串对的哈希表中为特定键计算缺失映射的示例

以下示例演示了 Java Hashtable computeIfAbsent() 方法的使用,以获取哈希表的更新值。我们创建了两个整数、字符串对的哈希表对象。然后向哈希表中添加一些条目,然后使用 computeIfAbsent() 方法更新值,然后打印更新后的哈希表。

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.computeIfAbsent(2, (key) -> {return "B";});

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

输出

让我们编译并运行上述程序,这将产生以下结果。

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

在整数、对象对的哈希表中为特定键计算缺失映射的示例

以下示例演示了 Java Hashtable computeIfAbsent() 方法的使用,以获取哈希表的更新值。我们创建了两个整数、Student 对的哈希表对象。然后向哈希表中添加一些条目,然后使用 computeIfAbsent() 方法更新值,然后打印更新后的哈希表。

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.computeIfAbsent(2, (key) -> {return new Student(2, "Robert");});
      
      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 ], 2=[ 2, Robert ], 1=[ 1, Julie ]}
java_util_hashtable.htm
广告
© . All rights reserved.