Get Set view of Keys from Java HashMap example

  1. /*
  2.   Get Set view of Keys from Java HashMap example
  3.   This Java Example shows how to get a Set of keys contained in HashMap
  4.   using keySet method of Java HashMap class.
  5. */
  6.  
  7. import java.util.Iterator;
  8. import java.util.HashMap;
  9. import java.util.Set;
  10.  
  11. public class GetSetViewOfKeysFromHashMapExample {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. //create HashMap object
  16. HashMap hMap = new HashMap();
  17.  
  18. //add key value pairs to HashMap
  19. hMap.put("1","One");
  20. hMap.put("2","Two");
  21. hMap.put("3","Three");
  22.  
  23. /*
  24.   get Set of keys contained in HashMap using
  25.   Set keySet() method of HashMap class
  26.   */
  27.  
  28. Set st = hMap.keySet();
  29.  
  30. System.out.println("Set created from HashMap 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 HashMap.
  38.   Any key that is removed from Set will also be removed from
  39.   original HashMap 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 HashMap still contains 2
  47. boolean blnExists = hMap.containsKey("2");
  48. System.out.println("Does HashMap contain 2 ? " + blnExists);
  49. }
  50. }
  51.  
  52. /*
  53. Output would be
  54. Set created from HashMap Keys contains :
  55. 3
  56. 2
  57. 1
  58. Does HashMap contain 2 ? false
  59. */

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!