Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Container/HashMap question

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
Hi,

I have a situation in which I need to be able to retrieve all keys associated with a particular value in a HashMap. However, I don't believe the class HashMap provides any methods to do this. I know I can obtain an EntrySet and then iterate through that, but I would like to know if there is a more "accepted" way to do this - perhaps a collection class or technique I'm unaware of.

Thanks, any help appreciated!!!
dora
 
I think an iterator would be the most straightforward way to do it.

If you have to do it a lot (in a loop), execution speed might be a factor. In that case you might consider maintaining a second HashMap keyed on the value of the first one and containing the number of duplicate entries in the first HashMap. You'd still have to build it once, but thereafter the access would be fast. But I would only consider doing that in a refactoring phase to optimize the process - if needed. "When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top