1

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

Community
  • 1
  • 1
Addev
  • 31,819
  • 51
  • 183
  • 302

1 Answers1

2

Why not create 2 maps, each one from the key to collection of values:

Map<Foo, Collection<Bar>> and Map<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

Aviram Segal
  • 10,962
  • 3
  • 39
  • 52
  • The alternative is of course to make a lookup function. Doing double bookkeeping is always a risk. If you must then make a class that wraps the two data structures, so you can test is properly. – Thirler Dec 05 '12 at 08:55
  • From what I understood the relations are Bi-Directional, meaning x->y is not equivilang to y->x so there is no double bookkeeping. – Aviram Segal Dec 05 '12 at 10:04