Get Head Map from Java TreeMap example

  1. /*
  2.   Get Head Map from Java TreeMap example
  3.   This Java Example shows how to get the portion of TreeMap whose keys are less than
  4.   the specified key using headMap method of Java TreeMap class.
  5. */
  6.  
  7. import java.util.SortedMap;
  8. import java.util.TreeMap;
  9.  
  10. public class GetHeadMapFromTreeMapExample {
  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 a Head Map from Java TreeMap use,
  26.   SortedMap headMap(Object toKey) method of Java TreeMap class.
  27.  
  28.   This method returns the portion of TreeMap whose keys are less than
  29.   toKey.
  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.headMap("3");
  37.  
  38. System.out.println("Head Map Contains : " + sortedMap);
  39.  
  40. /*
  41.   Please also note that,
  42.   - SortedMap throws IllegalArgumentException for any attempts to insert the
  43.   key grater than or equal to toKey.
  44.  
  45.   - subMap throws ClassCastException, if toKey can not be compared using
  46.   Map's Comparators
  47.   */
  48.  
  49.  
  50. }
  51. }
  52.  
  53. /*
  54. Output would be
  55. Head Map Contains : {1=One, 2=Two}
  56. */

Thanx

Great examples

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!