Get collection values from hashtable example

1.Basic java collection framework example program to get Value collection from hashtable

  •   Collection values()   This method used to get collection of values
  •  Important note is that if any value is removed from set the original hashtable key also removed
  1. package com.setviewHashtable;
  2.  
  3. import java.util.Hashtable;

  4. import java.util.Enumeration;
  5. import java.util.Iterator;
  6. import java.util.Set;
  7.  
  8. public class HashtableExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12.  //create Hashtable object
  13.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
  14.        
  15. //add key value pairs to Hashtable
  16. hashtable.put("1","Java Interview Questions");
  17. hashtable.put("2","Java Interview Programs");
  18. hashtable.put("3","Concept and example program");
  19. hashtable.put("4","Concept and interview Questions");
  20. hashtable.put("5","Java Quiz");
  21. hashtable.put("6","Real time examples");
  22.  
  23.  
  24.  Collection c = hashtable.values();
  25.  System.out.println("Values of Collection created from Hashtable are :");
  26. //iterate through the Set of keys
  27.  
  28.  Iterator itr = c.iterator();
  29.  while(itr.hasNext())
  30.  System.out.println(itr.next());
  31.            
  32.  c.remove("Java Quiz");
  33.            
  34. System.out.println("Elements in hash table");
  35. Enumeration e=hashtable.elements();
  36.         
  37.  
  38.  while (e.hasMoreElements()) {
  39.         System.out.println(e.nextElement());
  40. }    

  41. }
  42.  
  43. }
     



Output:

  1. Values of Collection created from Hashtable are :
  2. Real time examples
  3. Java Quiz
  4. Concept and interview Questions
  5. Concept and example program
  6. Java Interview Programs
  7. Java Interview Questions
  8. Elements in hash table
  9. Real time examples
  10. Concept and interview Questions
  11. Concept and example program
  12. Java Interview Programs
  13. Java Interview Questions


Java Collections example program Get Set view of Keys from Hashtable example

1.Basic java collection framework example program to get set view from hashtable

  • Set keySet()   This method used to get set view of the keys of hashtable
  •  Important note is that if any key is removed from set the original hashtable key also removed
  1. package com.setviewHashtable;
  2.  
  3. import java.util.Hashtable;

  4. import java.util.Enumeration;
  5. import java.util.Iterator;
  6. import java.util.Set;
  7.  
  8. public class HashtableExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12.  //create Hashtable object
  13.        Hashtable<String,String> hashtable = new Hashtable<String,String>();
  14.        
  15. //add key value pairs to Hashtable
  16. hashtable.put("1","Java Interview Questions");
  17. hashtable.put("2","Java Interview Programs");
  18. hashtable.put("3","Concept and example program");
  19. hashtable.put("4","Concept and interview Questions");
  20. hashtable.put("5","Java Quiz");
  21. hashtable.put("6","Real time examples");
  22.  
  23.  
  24.  Set st = hashtable.keySet();       
  25.  System.out.println("Set created from Hashtable Keys contains :");
  26. //iterate through the Set of keys
  27.  
  28.  Iterator itr = st.iterator();
  29.  while(itr.hasNext())
  30.  System.out.println(itr.next());
  31.            
  32.  st.remove("1");
  33.            
  34. System.out.println("Elements in hash table");
  35. Enumeration e=hashtable.keys();
  36.         
  37.  
  38.  while (e.hasMoreElements()) {
  39.         System.out.println(e.nextElement());
  40. }    

  41. }
  42.  
  43. }
     



Output:

  1. Set created from Hashtable Keys contains :
  2. 6
  3. 5
  4. 4
  5. 3
  6. 2
  7. 1
  8. Elements in hash table
  9. 6
  10. 5
  11. 4
  12. 3
  13. 2

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

Remove key value pair from hashtable java example

1.Basic java example program to iterate keys of hashtable
  • Object remove(Object key)    This method used to remove value pair 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.            
  28. // display search result
  29.  while (e.hasMoreElements()) {
  30.         System.out.println(e.nextElement());
  31. }    

  32. }
  33.  
  34. }
     



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

Iterate through values of hashtable java

1.Basic java example program to iterate keys of hashtable
  • Enumeration elements()   This method used to get values of hashmap

  1. package com.iteartekeysHashtable;
  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.  
  22.  
  23. Enumeration e=hashtable.elements();
  24.           
  25.  
  26.            
  27. // display search result
  28.  while (e.hasMoreElements()) {
  29.         System.out.println(e.nextElement());
  30. }    

  31. }
  32.  
  33. }
     



Output:

  1. Real time examples
  2. Java Quiz
  3. Concept and interview Questions
  4. Concept and example program
  5. Java Interview Programs
  6. Java Interview Questions

Iterate through keys of hashtable java

1.Basic java example program to iterate keys of hashtable
  • Enumeration keys()   This method used to get keys of hashmap

  1. package com.iteartekeysHashtable;
  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 exampe program");
  17. hashtable.put("4","Concept and interview Questions");
  18. hashtable.put("5","Java Quiz");
  19. hashtable.put("6","Real time examples");
  20.  
  21.  
  22.  
  23. Enumeration e=hashtable.keys();
  24.           
  25.  
  26.            
  27. // display search result
  28.  while (e.hasMoreElements()) {
  29.         System.out.println(e.nextElement());
  30. }    

  31. }
  32.  
  33. }
     



Output:

  1. 6
  2. 5
  3. 4
  4. 3
  5. 2
  6. 1

Check particular value exist in hashtable

1.Basic java example program to check particular value exist in hashtable
  • boolean contains(Object value)   This method used to check  particular value exist in hashtable

  1. package com.CheckElelemtHashtable;
  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 exampe program");
  17. hashtable.put("4","Concept and interview Questions");
  18. hashtable.put("5","Java Quiz");
  19. hashtable.put("6","Real time examples");
  20.  
  21.  /*
  22.  To check whether a particular value exists in Hashtable we need to use
  23.  boolean containsKey(Object value) method of Hashtable class.
  24. if the Hashtable contains mapping for specified value It returns true
  25. otherwise returns false.
  26. */
  27.        
  28.  boolean isExists = hashtable.contains("Java Quiz");
  29.  System.out.println("Java Quiz exists in Hashtable ? : " + isExists);

  30. System.out.println("Hashtable values : ");
  31.  
  32. Enumeration e=hashtable.elements();
  33.           
  34.  
  35.            
  36. // display search result
  37.  while (e.hasMoreElements()) {
  38.         System.out.println(e.nextElement());
  39. }    

  40. }
  41.  
  42. }
     



Output:

  1. Java Quiz exists in Hashtable ? : true
  2. Hashtable values : 
  3. Real time examples
  4. Java Quiz
  5. Concept and interview Questions
  6. Concept and exampe program
  7. Java Interview Programs
  8. Java Interview Questions
Select Menu