java.util.Hashtable.replaceAll() 方法用于将此哈希表中每个条目的值替换为以下结果在该条目上调用给定函数,直到处理完所有条目或函数抛出异常。
语法
public void replaceAll(BiFunction<? super K,? super V,? extends V> function)
这里,K和V分别是维护的键和值的类型
参数
函数 | 指定应用于每个条目的函数。 |
返回值
void 类型。
异常
无。示例:
在下面的示例中,java.util.Hashtable.replaceAll() 方法用于替换通过调用给定函数获得给定哈希表。
import java.util.*;
public class MyClass {
public static void main(String[] args) {
//创建哈希表
Hashtable<Integer, String> Htable = new Hashtable<Integer, String>();
//填充哈希表
Htable.put(101, "John");
Htable.put(102, "Marry");
Htable.put(103, "Kim");
Htable.put(104, "Jo");
Htable.put(105, "Sam");
//打印哈希表的内容
System.out.println("Htable contains: " + Htable);
//用大写值替换键的值
Htable.replaceAll((k, v) -> v.toUpperCase());
//打印哈希表的内容
System.out.println("Htable contains: " + Htable);
}
}
上述代码的输出将是:
Htable contains: {105=Sam, 104=Jo, 103=Kim, 102=Marry, 101=John}
Htable contains: {105=SAM, 104=JO, 103=KIM, 102=MARRY, 101=JOHN}