This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to use a HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use a HashMap" Watch "How to use a HashMap" New topic
Author

How to use a HashMap

Melanie Miller
Greenhorn

Joined: Jul 24, 2002
Posts: 13
I need to gain some understand on how to use
a HashMap. Does anyone know how to use one?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

A HashMap is frequently used when you want to associate data of some kind (must be an object) with a specific key. A lookup of the keyed data is as simple as a single get() call on the hash map instance.
The key can be any object, although Strings are frequently used. Be aware that to use an object of your own class as the key, you must be diligent about over-riding hashMap() and equals() from the Object class (anyone else know of any other potential pitfalls?)
What did you have in mind for its use?
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
For an understanding of how a Map data structure works, take a look at Chapter 12, Section 3 of David J. Eck's Introduction to Programming Using Java.
For information on using a Map, take a look at The Map Interface part of The Collections Trail of Sun's Java Tutorial.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
 
subject: How to use a HashMap