• Post Reply Bookmark Topic Watch Topic
  • New Topic

When do we use a Map?  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,


When do we choose to use a Map. We have List and Set which solves most of the data-structure problems then what is the thing that Set and List fail to address and Map does it nicely?


Thanks all.

Guy
 
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I asked you to write a program that contains people's names, and their telephone numbers, and if I entered a name in a search field, it would give me the phone number belonging to that person (assuming unique names), how would you do it?

[edit]

To make it more concrete, how would you implement these two methods? Feel free to add fields to the class.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the more abstract, whenever you want to associate data values with object keys.
 
Guy Emerson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst asked
To make it more concrete, how would you implement these two methods? Feel free to add fields to the class.
view plaincopy to clipboardprint?
final class TelephoneDirectory {

void addPhoneNumber(String name, String phone) {
//
}

String getPhoneNumber(String name) {
//
}
}




I guess yes here Map would be a good choice.


Okay; is this the only use that I need to remember or there is any other more complex use of Map that I need to know?


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maps are used all over the place for all sorts of things. Basically, as I already said, whenever you want to be able to look up a value given another value to use as a key. It'd be impossible to list all possible uses.
 
Guy Emerson
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is a situation where in a database there are multiple records stored on the basis of Primary key And I would need to fetch the records in a java program.
Then my solution would be to fetch the primary keys in the database and store them in a Map as keys and the rest of the fields of each record against each primary key be stored as an array of Strings and added to the Map against each key.

Am I correct?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You'd use SQL to fetch the data you need, and iterate over the returned data directly. Using a Map would duplicate the efforts already provided by the RDBMS.

When you do use a map (or any other collection or generic type), make sure to use proper type arguments.

In the example I had you code, you should use a Map<String, String>, not just Map.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guy Emerson wrote:Okay; is this the only use that I need to remember or there is any other more complex use of Map that I need to know?

Well, apart from what the others have told you, there is one very important thing to know about Maps:

They impose a 1:1 mapping between keys and values.

Which is a fancy way of saying that they do NOT allow duplicate keys ('duplicate' being defined by either equals() or compareTo(), depending on what kind of Map it is).

Thus, the telephone directory example would not allow more than one "John Smith".

This can be circumvented by making the 'value' a List, or using Apache's MultiMap class, but it does make single-value access a little more involved.

HIH.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!