• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Map, Hashmap: when would one use them?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 34
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic