How to delete all elements from Hashtable

import java.util.Hashtable;

public class HashtableDeleteAll {

public static void main(String[] args) {

Hashtable<Integer,String> ht = new Hashtable<Integer,String>();

/*
* Maps the specified key to the specified
* value in this hashtable
*/
ht.put(1, “java”);
ht.put(2, “jersey”);
ht.put(3, “spring”);
ht.put(4, “c”);
ht.put(5, “php”);

//print hashtable
System.out.println(“Before delete Hash Table is =”+ht);

//Clears this hashtable so that it contains no keys.
ht.clear();

System.out.println(“After delete all element hashtable is = “+ht);

}

}

Output:

Before delete Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

After delete all element hashtable is = {}

Leave a Reply

Your email address will not be published. Required fields are marked *

11 − 10 =