Java.util.Hashtable 类

java.util.Hashtable.remove() 方法用于删除指定键的条目(仅当它是)当前映射到指定值。

语法

public boolean remove(Object key, Object value)

参数

key 指定与指定值关联的键。
指定预期与指定值关联的值key。

返回值

如果值被删除,则返回 true。

异常

不适用。

示例:

在下面的示例中,java.util.Hashtable.remove() 方法是用于从给定哈希表中删除指定键的映射。

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");

    //打印哈希表
    System.out.println("Before remove, Htable contains: " + Htable);    

    //删除102键的映射
    Htable.remove(102, "Marry");
    //删除 103 键 - 没有效果
    //指定的值不匹配
    Htable.remove(103, "Sam");

    //打印哈希表
    System.out.println("After remove, Htable contains: " + Htable);  
  }
}

上述代码的输出将是:

Before remove, Htable contains: {104=Jo, 103=Kim, 102=Marry, 101=John}
After remove, Htable contains: {104=Jo, 103=Kim, 101=John}