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 ]