public V remove(Object key): Removes the key and its corresponding value from the Hashtable table and returns the value of the key that was deleted, otherwise null if the key is not found.
An exception nullPointerException is thrown if the key is null.
This code looks for an item with its key and removes the key-value pair from the Hashtable, and then it displays the list before and after removal:
import java.util.Hashtable;Runtime:
import java.util.Map;
public class main{
public static void main(String[] args) {
Hashtable< String, String> ht = new Hashtable< String, String> ();
ht.put("1", "one");
ht.put("2", "two");
ht.put("3", "three");
Object obj = ht.remove("2");
System.out.println(obj + " has been removed");
System.out.println("HashSet after deletion:");
for (Map.Entryentry : ht.entrySet())
{
System.out.println("<" +entry.getKey()+", "+entry.getValue()+">");
}
}
}
two was deletedReferences:
HashSet after removal:
< 3, Three>
< 1, A>
JavaDoc: Hashtable remove method
Commentaires (0)
Laisser un commentaire
Connectez-vous pour commenter
Rejoignez la discussion et partagez vos connaissances avec la communauté
Chargement des commentaires...