Re: Indexing by multiple keys
On 31.07.2009 23:35, nooneinparticular314159@yahoo.com wrote:
I would like to use an object that behaves like a hashmap (ie. put,
get, with a key), but I'd like to be able to index items in that
object by more than one key.
Is there a way to do this?
Ie. if I had a name for the objects I'm adding as well as a social
security number, I'd like to be able to return the object by either
name or number depending on need. Is there an object that does this,
or do I have to write it myself?
You will need multiple Maps for that. Even though you could stuff
everything in a single Map I'd rather not do this because then you
cannot manipulate indexes individually any more. I would probably write
my own class which does all the Map handling and ensuring consistency
internally.
If you need this in multiple places with different data types and index
values it may pay off to look for a generic solution to the problem or
write it yourself (probably using reflection).
If the properties of objects which you use as keys are allowed to
change, things get more complicated. You probably then would have to
use some form of observer pattern to notify your multi index class of
the change. At the very least you would need a reindex operation which
can be invoked manually.
The fun starts, if you mix both (generic solution and mutable key
fields). :-)
Another interesting question is whether your indexes are all unique or
non unique and what happens if you have a unique index and get two
objects with equivalent key values...
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/