Skip to Content

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