Rabu, 16 Maret 2016

Remove all elements from hashtable java example

1.Basic java example program to remove all elements from hashtable
  • Object remove(Object key)    This method used to remove value pair from hashtable
  • void clear() this method removes all elements from hashtable
  •  
  1. package com.removevalueHashtable;
  2.  
  3. import java.util.Hashtable;

  4. import java.util.Enumeration;
  5.  
  6. public class HashtableExample{
  7.  
  8. public static void main(String[] args) {
  9.   
  10.  //create Hashtable object
  11.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
  12.        
  13. //add key value pairs to Hashtable
  14. hashtable.put("1","Java Interview Questions");
  15. hashtable.put("2","Java Interview Programs");
  16. hashtable.put("3","Concept and example program");
  17. hashtable.put("4","Concept and interview Questions");
  18. hashtable.put("5","Java Quiz");
  19. hashtable.put("6","Real time examples");
  20.  
  21. Object obj = ht.remove("2");
  22. System.out.println(obj + " Removed from Hashtable");
  23.  
  24. Enumeration e=hashtable.elements();
  25.           
  26.  
  27.  while (e.hasMoreElements()) {
  28.         System.out.println(e.nextElement());
  29. }    
  30. hashtable.clear();
  31.  
  32.  System.out.println("Total key value pairs in Hashtable are : " + hashtable.size());

  33. }
  34.  
  35. }
     



Output:

  1. Java Interview Programs Removed from Hashtable
  2. Real time examples
  3. Java Quiz
  4. Concept and interview Questions
  5. Concept and exampe program
  6. Java Interview Questions
  7. Total key value pairs in Hashtable are : 0

Bagikan

Jangan lewatkan

Remove all elements from hashtable java example
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.