I have two classes, ClassA
and ClassB
, as well as a "many to many" AssociationClass
. I want a structure that holds the associations between A and B so that I can find the counterpart for each instance of A or B.
I thought of using a Hashmap, with pair keys:
Hasmap<Pair<ClassA, ClassB>, AssociationClass> associations;
This way, I can add and remove an association between two instances of ClassA
and ClassB
, and I can query a relation for two given instances.
However, I miss the feature of getting all associations defined for a given instance of ClassA
or ClassB
.
I could do it by brute force and loop over all keys of the map to search for associations between a given instance, but this is inefficient and not elegant.
Do you know of any data structure / free library that enables this? I don't want to reinvent the wheel.
NB: This is not a "database" question. These objects are pure POJO used for live computation, I don't need persistence stuff.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…