• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which Java collection would be the best suited.  RSS feed

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • 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?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
salvin francis
Bartender
Posts: 1660
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!