Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

phaelax

[java] HashMap alternative?

This topic is 5218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I have a HashMap with names for the keys and numbers for the values. Input the key and you get the associated number returned. But if I don''t know the name, but instead the number, I would have to have another HashMap with the keys and values reversed. My question is, is there a similar object that could have the same basic functions as a HashMap, but could take in a value and return the key as well as still returning a value for a key? Or am I stuck with two seperate HashMap objects?

Share this post


Link to post
Share on other sites
Advertisement
Well, I don''t know of any alternatives.....but with HashMaps, should the value have meaning on its own, regardless of the key? As far as I understand it, the key is merely there to allow you to reference the value. If your HashMap requires both value and key to create meaningful data, perhaps you should change what data is being stored.

Please correct me if I''m wrong about this

Share this post


Link to post
Share on other sites
I''m creating a MP3 database application. The genre of the music is stored in the file as number, 0-79. There''s a list of genres that are associated with each value. So far, it seems pretty simple what I have to do. But my application has a tag editor in it. It contains a combo box listing all the genre names. Unless there''s a way to get the index of the selected combo box item and I keep the data parallel with the map, I''ll need to get the number that points to that string the combo box returns.

Share this post


Link to post
Share on other sites
You have a generic indexing problem here. HashMaps are a reasonably effective way of achieving what you want. For any particular searchable field (or "index") you maintain a hashmap that maps that field to the object instance.

Cas

Share this post


Link to post
Share on other sites
quote:
Original post by levendis
I believe Jakarta Commons Collections has a bidirectional map interface that does what you want.

But it would be implemented with two hashtables(or trees or what have you) anyway.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!