• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A simple Hash Map Example.

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is a simple hash map example copied from a text book.
I understand the code, but what i don't follow is, the book says that the letters of the hash map are printed on screen "in the order in which the elements were inserted".
Running the program, i get the output: [D, A, F, C, B, E]
Why is this so?


Also, as a general question, why would we need a hash map??

Any help would be appreciated.

Cheers in advance folks.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the book says that the letters of the hash map are printed on screen "in the order in which the elements were inserted".

If that is an accurate quote, it is an error in the book. Per the Java API for the HashMap class: "It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time." The only way (I am familiar with) of getting the results in a Map back in the same order you added is to use the LinkedHashMap (or LinkedHashSet for a Set) added in Java 1.4 (A TreeSet or TreeMap can be used to maintain a sorting order, such as alphabetical).

Also, as a general question, why would we need a hash map??


First off, be careful not to confuse a HashSet and a HashMap; your example uses a HashSet, but here you are asking about a HashMap, and your book quote speaks of a Map. I am not sure if it was your intention to ask about a HashMap knowing your example used a HashSet, or if you (or your book ) are mistakenly using the two interchangeably. They are very different in that one use a Map interface (key=value pairs) and the other uses the Set interface.
The answer to your question, in a very basic sense, is you would use the HashMap when you want a collection implementation (i.e. have a problem beset solved by a collection) that uses a HashTable storage paradigm (fast random access) and that uses a Map Interface (maps key to value pairs and cannot contain duplicate keys). HashMaps are good when you need a collection to store items in a key=value mode, do not need (or want) duplicate keys entries, and want fast random access (HashMap provides the fastest random access of all the Map classes).
Take a look at the Sun Collections Tutorial for a nice overview of the differences between the various types of collections. For a more involved Tutorial, look at Sun's Introduction to the Collections Framework
Regards,
Mark
[ January 03, 2004: Message edited by: Mark Vender ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic