• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I get the key of the HashMap?  RSS feed

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here getEmployeeID() get the Employee name instead of the ID, how do I get the ID(key) instead of the value?
Thanks a lot
Jack
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't directly. You need to loop through the keySet or Map to get the keys. However you can check if the key exists in the map using containsKey().
 
srinivasa raghavan ambikapathy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jacky,
In your case if the key exists, wouldn't this.cbEmployeeID.getSelectedItem() be the employee id? can you elaborate your question a bit?

-Srinivas
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srinivasa raghavan ambikapathy wrote:Hi Jacky,
In your case if the key exists, wouldn't this.cbEmployeeID.getSelectedItem() be the employee id? can you elaborate your question a bit?

-Srinivas


Hi there,
I've solved it by
using a
HashMap<Integer, Employee>
in which Integer is the index of the combo box, and Employee follows the index, so there won't be a selection problem because
they are related now.
Thanks
Jack
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacky Luk wrote:I've solved it by using a
HashMap<Integer, Employee>
in which Integer is the index of the combo box, and Employee follows the index, so there won't be a selection problem because they are related now.

Any reason you couldn't have just used a List? Or even an array? There might be another reason why you need a HashMap, but it smells a bit "over-engineered" to me.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!