• 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

telephone directory & collections ...

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi !
I saw a mock exam question somewhere asking what type of collection should be used in the case of a telephone directory.
In my opinion, a telephone directory is a duplicating (there are many cases of people having the exact same name), sorted collection.
So I chose a Vector collection (with the assumption that it will be sorted later), but the answer was TreeMap.
But if TreeMap does not allow duplicate key values, why is this the correct answer ?
Thanks !
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set is the interface that does not allow duplicates. So a Map is still a choice. Since you need key/value pair and needed it sorted, TreeMap is the only choice. Can you imagine a directory in a Vector? it'll be very inefficient.
[ July 01, 2003: Message edited by: Andres Gonzalez ]
 
bennido kool kat
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that Vector is VERY inefficient, but it seemed like the only choice left.
Sets do not allow duplicates so that it out. Maps do not allow duplicate key values so that it out too.
For example, if there are 2 John Doe's in the directory ...
"John Doe", "12345678"
"John Doe", "87654321"
... only the latter one will be left in the Map.
So Maps will not work.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u're right...
check this
http://access1.sun.com/techarticles/CollectionsMap.html#implementationTreemap
I'll do a bit more research on that.. but if I have no choice, I would choose treemap over all others..
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The content of a telephone directory changes frequently.
It would be easier to add an entry in sorted order to a linked list than to an array list or vector.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need duplicate keys? Use one key for the name and an array (or more likely an ArrayList) containing the various phone numbers associated with that name as the value:
Key = "John Doe", Value = {"12345678", "87654321"}
[ July 01, 2003: Message edited by: Thomas Paul ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am having one doubt. Can't we make telephone number the Key of the TreeMap?
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am having one doubt. Can't we make telephone number the Key of the TreeMap?


Then how would you retriev the phone number of a person? You would have to walk arround all the names (i.e. all the values of your TreeSet). And remeber, a TreeSet has the keys ordered, NOT the values.

Why do you need duplicate keys? Use one key for the name and an array (or more likely an ArrayList) containing the various phone numbers associated with that name as the value:
Key = "John Doe", Value = {"12345678", "87654321"}


What bennido suggests is that he wants to have more persons (DIFFERENT persons) having the same name (and obviously having different phone numbers). Not one person having more numbers.
Generally I think the question "makes" the assumption that all persons have different name. Otherwise (if there are different persons with the same name) you could still use a TreeMap (and is best to use anyway) in which you have to think of a different key. Like for e.g. a Value class consisting of both name and address (in which you will have to overload the equals() and hashCode() metods since you want to use it in a TreeMap).
Miki
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Miki Muzsi:
What bennido suggests is that he wants to have more persons (DIFFERENT persons) having the same name (and obviously having different phone numbers). Not one person having more numbers.


My version has different persons with the same name (and therefore the same key) having different phone numbers. Obviously unless you add addditional information such as address there is no way to tell one John Doe from another in either system. But if you do add additional information, then what goes into the ArrayList wouldn't be a phone number String but rather a Customer object containing all the information you want to carry about the customer.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Deepak Nagpal:
I am having one doubt. Can't we make telephone number the Key of the TreeMap?


Only if you want to do a reverse lookup.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
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!