Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map, Hashmap: when would one use them?

 
Kiley smith
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Kiley
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
Badal
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just seen I wrote get(new Address yesterday. That should have been new Name ...

Sorry for the mistake.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Kiley
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic