Skip to Content

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