从哈希表中删除元素
您可以删除哈希表的元素,您可以使用 HashTable 类的 remove() 方法。
对于此方法,您需要传递键或键值对以删除所需的元素。
hashTable.remove("Ram"); or hashTable.remove("Ram", 94.6);
示例
import java.util.Hashtable; public class RemovingElements { public static void main(String args[]) { Hashtable hashTable = new Hashtable(); hashTable.put("Ram", 94.6); hashTable.put("Rahim", 92); hashTable.put("Robert", 85); hashTable.put("Roja", 93); hashTable.put("Raja", 75); System.out.println("Contents of the hash table :"+hashTable); hashTable.remove("Ram"); System.out.println("Contents of the hash table after deleting the specified elements :"+hashTable); } }
输出
Contents of the hash table :{Rahim = 92, Roja = 93, Raja = 75, Ram = 94.6, Robert = 85} Contents of the hash table after deleting the specified elements :{Rahim = 92, Roja = 93, Raja = 75, Robert = 85}
广告