In article < >,
(vicky) wrote:
> I want to know if there is any efficient way to implement something
> ;like a hashmap which has the entries in the form
> (key,valu1,value2).The structure should also work for getting the key
> using any one of the values.
>
> any thought on this?? is there any java libraries which implements
> this??
>
> -vick
Use three maps, each with a different object as the key, and all three
sharing the same result Object.
class FooElement
{
final Object key;
final Object value1;
final Object value2;
public FooElement (Object key, Object value1, Object value2)
{
this.key= key;
this.value1= value1;
this.value2= value2;
}
}
final HashMap m_keyMap= new HashMap ();
final HashMap m_value1Map= new HashMap ();
final HashMap m_value2Map= new HashMap ();
void put (Object key, Object value1, Object value2)
{
FooElement f= new FooElement(key, value1, value2);
m_keyMap(key, f);
m_value1Map(value1, f);
m_value2Map(value2, f);
}
Object getKeyByValue1 (Object value1)
{
FooElement f= (FooElement)m_value1Map.get (value1);
return (f == null) ? null : f.key;
}
// etc...
This is a somewhat tedious example so adjust it to suit your needs.
You'll also need to figure out what happens when the mappings aren't
1:1. If your mapping needs to be large, complex, rule-based, shared, or
have persistence, a database is exactly what you'll want. The code gets
to be a mess beyond simple cases like I've shown above.