[java] HashMap alternative?

Started by
4 comments, last by phaelax 20 years, 1 month ago
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?
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
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.
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
I believe Jakarta Commons Collections has a bidirectional map interface that does what you want.
------http://www.livejournal.com/users/rain_is_wet/
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.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement