This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Map, Hashmap: when would one use them? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map, Hashmap: when would one use them?" Watch "Map, Hashmap: when would one use them?" New topic

Map, Hashmap: when would one use them?

Kiley smith

Joined: Jun 17, 2011
Posts: 27
Hi there,

I have a concept question about maps and hashmaps. I'm learning about them and my over-riding question is, "when would I use something like this?"

What is a real-world scenario in which one uses a map over, say, a vector?

Many thanks,
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

In cases where you need a key-value data-structure. For instance HashMaps are often used for lookup. E.g. you've got a person-id and with the HashMap (or an other Map implementation) you can lookup the corresponding Person object. You could iterate through a List but the performance of a HashMap is much better.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37907
Imagine an address list. You could set that up with a Map<Name, Address>. In fact it is exactly the thing Maps are good for.
Badal Chowdhary
Ranch Hand

Joined: Apr 26, 2011
Posts: 34

Think of Map like table of contents where there is a mapping between Chapter and its page number. So if you wanted to read what is there is Chapter 5, you can gets it page number from Map and directly go there.

If you are using a Vector or List, you would have to browse through Chapters 1-4 to reach Chapter 5.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37907
I have just seen I wrote get(new Address yesterday. That should have been new Name ...

Sorry for the mistake.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

HashMap is famous for its algorithm called *hashing*. this technique is very fast for adding and finding/retrieving an element in the array
Kiley smith

Joined: Jun 17, 2011
Posts: 27
Thank you to everyone for your replies, this is helping.

When retrieving information from a map, is the goal to get an array index or the actual corresponding value? I suppose one would look for 1) presence: if the item you are looking for is really there and then 2) the matching value.

Does this seem right?

Thank you,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

A Map is a data structure that you use to quickly find values by key - in other words, you know the key, and you want to lookup the value.

Just like a telephone directory: you know a person's name (the key) and you're looking for his or her telephone number (the value).

There's nothing more complicated to it, with array indices or anything like that.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
subject: Map, Hashmap: when would one use them?
Similar Threads
When should I be using Service and BroadcastReceiver?
Map or HashMap?
A simple Hash Map Example.
Single table / Simple Locking - WeakHashMap vs WeakReferences
Application of URL!