Skip to Content

Get Sub Map 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 Sub Map from Java TreeMap example
  3.   This Java Example shows how to get the sub Map from Java Treemap by giving specific
  4.   range of keys using subMap method of Java TreeMap class.
  5. */
  6.  
  7. import java.util.TreeMap;
  8. import java.util.SortedMap;
  9.  
  10. public class GetSubMapFromTreeMapExample {
  11.  
  12.   public static void main(String[] args) {
  13.  
  14.     //create TreeMap object
  15.     TreeMap treeMap = new TreeMap();
  16.    
  17.     //add key value pairs to TreeMap
  18.     treeMap.put("1","One");
  19.     treeMap.put("3","Three");
  20.     treeMap.put("2","Two");
  21.     treeMap.put("5","Five");
  22.     treeMap.put("4","Four");
  23.    
  24.     /*
  25.       To get the sub Map from Java TreeMap use,
  26.       SortedMap subMap(int fromKey, int toKey) method of TreeMap class.
  27.      
  28.       This method returns portion of the TreeMap whose keys range from
  29.       fromKey (inclusive) to toKey(exclusive).
  30.      
  31.       Please note that, the SortedMap returned by this method is backed by
  32.       the original TreeMap. So any changes made to SortedMap will be
  33.       reflected back to original TreeMap.
  34.     */
  35.    
  36.     SortedMap sortedMap = treeMap.subMap("2","5");
  37.    
  38.     System.out.println("SortedMap Contains : " + sortedMap);
  39.    
  40.     /*
  41.       Please also note that,
  42.       - SortedMap throws IllegalArgumentException for any attempts to insert the
  43.       key less than fromKey and grater than or equal to toKey.
  44.      
  45.       - subMap throws ClassCastException, if fromKey and toKey can not be
  46.       compared using Map's Comparators
  47.      
  48.       - subMap throws IllegalArgumentException, if fromKey is grater than toKey
  49.     */
  50.    
  51.    
  52.   }
  53. }
  54.  
  55. /*
  56. Output would be
  57. SortedMap Contains : {2=Two, 3=Three, 4=Four}
  58. */