• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting keys from values in hashmap  RSS feed

 
naveen sakhare
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have one doubt.In HashMap if keys contains 1,2,3,4 and values are a,b,c,d we can get values using get(key) method like 1 will A,2 will return B and so on. Can we get the keys from values like A will get 1 and also if in key if there is a String like 1,2,3,Z and value is A,B,C,7 Z should get me 7.Here I am not using any generics.

Please help.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look into the Commons Collection http://commons.apache.org/proper/commons-collections/ library.

Here is a sample:



With each iteration you can get a key and a value. I'm not sure though if this is what you are looking for. Have a look at the Commons Collection library.
 
naveen sakhare
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:You might want to look into the Commons Collection http://commons.apache.org/proper/commons-collections/ library.

Here is a sample:



With each iteration you can get a key and a value. I'm not sure though if this is what you are looking for. Have a look at the Commons Collection library.


I was looking similar to this one.Thanks !
 
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

The standard HashMap class, which I presume the OP is asking about, also has an entrySet() method.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need constant speed reverse look up, a bi-directional map seems a good alternative. Commons should have BidiMap.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the design. Maps are designed to map K→V. If you need to go K→V and V→K consider whether you ought to have a class which encapsulates both K and V, as an alternative to using the bi‑directional Map.

And yes, there is a bi‑directional Map class available at Apache Commons.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naveen sakhare wrote:Can we get the keys from values like A will get 1 and also if in key if there is a String like 1,2,3,Z and value is A,B,C,7 Z should get me 7.

In addition to the other great advice, it might be worth mentioning that java.util.Map only enforces the requirement that keys are unique. It's therefore possible that, when going in the reverse direction, there may be more than one possible "value". I'm not familiar with BidirectionalMap, so I don't know how it deals with this situation; but if you plan on rolling your own, it's something to bear in mind.

It also probably means that reverse lookups won't necessarily be O(1).

HIH

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