• Post Reply Bookmark Topic Watch Topic
  • New Topic

Grouping employees on their names from a employee list  RSS feed

 
udaya krishna
Ranch Hand
Posts: 65
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When I recently attended an interview , I was asked to find the employees from a employeeList who have the same name.Fortunately I was not asked to code but I just told them that I would create a Map of employee object as key and an integer as value and I would override equals method so that i returns true when the two employees have same name.I came home and tried to code it but somehow I could not get the correct result.This is my code so far:
Employee.java

My class:


The statement
is printing true.
So empSameNameMap should not contain both e1 and e2 as they will be considered as duplicates.Yet they are added into the map.
May I know where I am going wrong?

Thanks a lot
Udaya Krishna A

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The keys of your empSameNameMap map are Employee objects. So when you ask whether a particular String is in that map, the answer is always going to be "No".
 
udaya krishna
Ranch Hand
Posts: 65
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham ,
Thanks for the reply.That was quite a bad mistake, I think I might have to change it to :


But I think I am making a mistake while printing the hashmap as well , because after changing the values that are printed are :

Thanks
Udaya Krishna
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why the key of that Map is an Employee anyway. Shouldn't it be the employee's name, since you're looking for duplicate employee names? And you don't seem to be using the Integer part of the map to store the frequency, either. Instead you're using some irrelevant counter variable.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, you are printing the map, while you are iterating / adding to the map. It is very difficult to tell the difference between iterations, and also the final result.

Henry
 
udaya krishna
Ranch Hand
Posts: 65
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with HashMap<String,Integer> and added

Frequency is to count number of employees.Its dummy for me right now.But I am unable to figure out why I am able to add two employees with same nam.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
udaya krishna wrote: I am unable to figure out why I am able to add two employees with same nam.


You're not. However, you have at least two bugs, one pointed out by Paul, one pointed out by me. And the combination of the two is giving you a confusing output, that you mistake as having duplicate keys in your map.

Henry
 
udaya krishna
Ranch Hand
Posts: 65
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its so hard to believe that I made such silly mistakes in so little code.I placed the braces correctly (before printing).Now its working fine .thank you all for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!