java.util.Hashtable.clear() Method

Advertisements


Description

The clear() method is used to clear this hashtable so that it contains no keys.

Declaration

Following is the declaration for java.util.Hashtable.clear() method.

public void clear()

Parameters

  • NA

Return Value

NA

Exception

  • NA

Example

The following example shows the usage of java.util.Hashtable.clear()

package com.tutorialspoint;

import java.util.*;

public class HashTableDemo {
   public static void main(String args[]) {
      // create hash table 
      Hashtable htable = new Hashtable();

      // put values into the table
      htable.put(1, "A");
      htable.put(2, "B");
      htable.put(3, "C");
      htable.put(4, "D");
      
      // check table content
      System.out.println("Hash table content: "+htable);
      
      // clear the table
      htable.clear();
      
      // check content after clear
      System.out.println("Hash table content after clear: "+htable);      
   }    
}

Let us compile and run the above program, this will produce the following result.

Hash table content: {4=D, 3=C, 2=B, 1=A}
Hash table content after clear: {}



Advertisements
Advertisements