wood burning stoves*
The moose likes Beginning Java and the fly likes A simple Hash Map Example. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A simple Hash Map Example." Watch "A simple Hash Map Example." New topic
Author

A simple Hash Map Example.

Steve Jensen
Ranch Hand

Joined: Sep 23, 2002
Posts: 126
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.


John Bonham was stronger, but Keith Moon was faster.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple Hash Map Example.
 
Similar Threads
Question on HashSet & LinkedHashSet
About iteration order of different data structure
HashSet Size
Generic data structures and wildcard types
Can we predict the iteration order of hashset?