Posts Tagged Collections

Java – How to clear HashTable

Tuesday, July 7th, 2009

All Elements in HashTable can be removed by clear() method. Clear() method in HashTable clear HashTable object and size of the HashTable will become 0

Example of clear in HashTable

import java.util.Hashtable;

public class HashTableClearExample {

    public static void main(String[] args) {

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

        hTable.put(new Integer(2), "Two");
        hTable.put(new Integer(1), "One");
        hTable.put(new Integer(4), "Four");

        hTable.clear(); // clear all elements in hashtable

        System.out.println("Size of hashTable :"+hTable.size());
    }
}

Output

Size of hashTable :0