CodesJava

Easy learning with example program codes

Remove all elements from hashtable in java


We can use clear() method to remove all elements from hashtable in java.

Syntax:

hashtable.clear();

Example:

package com.codesjava;
 
import java.util.Hashtable;
 
public class Test {
  public static void main(String args[]){
	//Create Hashtable object.
	Hashtable<String, String> hashtable = new Hashtable<String, String>();
	//Add objects to the Hashtable.
	hashtable.put("1", "Jai");
	hashtable.put("2", "Mahesh");
	hashtable.put("3","Vivek");
        System.out.println(hashtable);
        hashtable.clear();
        System.out.println("Content After clear:" + hashtable);
  }
}

Output

{3=Vivek, 2=Mahesh, 1=Jai}
Content After clear:{}
Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki