• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Which Java collection would be the best suited.

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

There is a particular scenario that I am trying to code and would need your help in deciding which Java collection would be the best to achieve it.

I have a collection of Employees and each employee can have multiple phone numbers.

So I should be able to pull up the employee details either by providing the employee name or by providing the employee Phone number.

Can you help me in figuring out which Java collection would be the best to achive this(Assume that we are not to use any Databases to store the data. Only java collection frameworks).

Thank you!
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have to take into account real world scenarios such as two or more different people having the same name or two or more different people having the same phone number?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Padma Priya wrote:I have a collection of Employees and each employee can have multiple phone numbers.


Well, if each phone number only applies to one Employee, what about a Map<PhoneNumber, Employee>?

Winston
 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

For now the relation is just one employee with one or many phone numbers.

So when we give the input as Employee name or phone name, the code should display that particular Employee details.

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Winston,

Well, if each phone number only applies to one Employee, what about a Map<PhoneNumber, Employee>?



In this case When i give the phone number, I can get the Employee details.

But when I give the Employee name how can i pull up the Employee details.

Can you please clarify.
 
Sheriff
Posts: 27458
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Padma Priya wrote:But when I give the Employee name how can i pull up the Employee details.



Well, if each name only applies to one Employee, what about a Map<String, Employee>?
 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Well, if each name only applies to one Employee, what about a Map<String, Employee>?



The datastructure that I select should be in such a way that if given the name or phone number the Employee details should be brought up.

Not sure if this is achiveable but wanted some expert advice on it.

Thank you!
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think a single data structure can fulfill both the requirements.

A java.lang.Map is the structure you want if you want to quickly lookup a value for a given key. So, your key can either be the phone number or the employee name, not both. Note that you can yet loop through a map Map.Entry if you want to additionally search for something else.

While I feel that a database query is more suited for such need, A quick and dirty solution would be to use two Maps here.
 
Paul Clapham
Sheriff
Posts: 27458
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:While I feel that a database query is more suited for such need, A quick and dirty solution would be to use two Maps here.



Nobody has mentioned databases yet, but a database would be the natural solution if this were a real-life implementation, wouldn't it? Have a cow for bringing up that idea!
 
Good night. Drive safely. Here's a tiny ad for the road:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic