Skip to Content

Get Set view of Keys from Java Hashtable example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.   Get Set view of Keys from Java Hashtable example
  3.   This Java Example shows how to get a Set of keys contained in Hashtable
  4.   using keySet method of Java Hashtable class.
  5. */
  6.  
  7. import java.util.Enumeration;
  8. import java.util.Iterator;
  9. import java.util.Hashtable;
  10. import java.util.Set;
  11.  
  12. public class GetSetViewOfKeysFromHashtableExample {
  13.  
  14.   public static void main(String[] args) {
  15.    
  16.     //create Hashtable object
  17.     Hashtable ht = new Hashtable();
  18.    
  19.     //add key value pairs to Hashtable
  20.     ht.put("1","One");
  21.     ht.put("2","Two");
  22.     ht.put("3","Three");
  23.    
  24.     /*
  25.       get Set of keys contained in Hashtable using
  26.       Set keySet() method of Hashtable class
  27.     */
  28.    
  29.     Set st = ht.keySet();
  30.    
  31.     System.out.println("Set created from Hashtable Keys contains :");
  32.     //iterate through the Set of keys
  33.     Iterator itr = st.iterator();
  34.     while(itr.hasNext())
  35.       System.out.println(itr.next());
  36.      
  37.     /*
  38.        Please note that resultant Set object is backed by the Hashtable.
  39.        Any key that is removed from Set will also be removed from
  40.        original Hashtable object. The same is not the case with the element
  41.        addition.
  42.     */
  43.    
  44.     //remove 2 from Set
  45.     st.remove("2");
  46.    
  47.     //print keys of original Hashtable
  48.     System.out.println("Hashtable keys after removal from Set are :");
  49.     Enumeration e = ht.keys();
  50.     while(e.hasMoreElements())
  51.       System.out.println(e.nextElement());
  52.   }
  53. }
  54.  
  55. /*
  56. Output would be
  57. Set created from Hashtable Keys contains :
  58. 3
  59. 2
  60. 1
  61. Hashtable keys after removal from Set are :
  62. 3
  63. 1
  64. */