• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

When do we use a Map?

 
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: 10849
234
  • 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.
 
Marshal
Posts: 67313
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67313
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 10849
234
  • 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: 10777
71
Hibernate Eclipse IDE 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
 
Get me the mayor's office! I need to tell him about this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!