• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dual key Hashmap  RSS feed

 
Pratik Lohia
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I need to have a hashmap, or any kind of indexed collection class which I can create and search using a dual key...
Any pointers?...
Thanks
Pratik
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never used it, but org.apache.commons.collections.keyvalue.MultiKey seems like a likely solution for you.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you just create your own key class that implements equals and hashcode accordingly?
 
Pratik Lohia
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help guys
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are certain that no key of type 1 could ever match a key of type 2 you could just put each object in the map twice, once with each key. This might work with state and zipcode where AK could not be a zipcode and 10010 could not be a state.

states.put( stateCode, state)
states.put( zipCode, state)
state = states.get( "AK" )
state = states.get( "10010" )

Any key collision would be fatally confusing, I'd expect.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!