• Java 資料結構教程

從雜湊表中刪除元素



您可以刪除雜湊表的元素,您可以使用 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}
廣告

© . All rights reserved.