Possible Duplicate:
Bidirectional multi-valued map in Java
I need a data structure that implements a N-to-N relation. Something like a Map<Foo,Bar> with calls:
getValues(Foo foo): Collection<Bar>
getValues(Bar bar): Collection<Foo>
and methods for the usual management like:
removeKey(Foo) [remove all the <Foo,X> entries]
removeValue(Bar) [remove all the <X,Bar> entries]
Is there some library I can use or should I implement it? Thanks
Why not create 2 maps, each one from the key to collection of values:
Map<Foo, Collection<Bar>>andMap<Bar, Collection<Foo>>If you wish you can create a class to wrap (Maybe use some generics for the types so you can easily reuse it) them inside and provide the methods you need