Java多对多关联映射
我有两个类,Java多对多关联映射,java,hashmap,structure,many-to-many,associations,Java,Hashmap,Structure,Many To Many,Associations,我有两个类,ClassA和ClassB,还有一个“多对多”AssociationClass。我想要一个保存a和B之间关联的结构,这样我就可以找到a或B的每个实例的对应项 我想到了使用一个Hashmap,带有成对的键: Hasmap<Pair<ClassA, ClassB>, AssociationClass> associations; Hasmap关联; 这样,我可以添加和删除ClassA和ClassB的两个实例之间的关联,并且可以查询两个给定实例的关系 但是,我忽
ClassA
和ClassB
,还有一个“多对多”AssociationClass
。我想要一个保存a和B之间关联的结构,这样我就可以找到a或B的每个实例的对应项
我想到了使用一个Hashmap,带有成对的键:
Hasmap<Pair<ClassA, ClassB>, AssociationClass> associations;
Hasmap关联;
这样,我可以添加和删除ClassA
和ClassB
的两个实例之间的关联,并且可以查询两个给定实例的关系
但是,我忽略了为ClassA
或ClassB
的给定实例定义所有关联的功能
我可以通过蛮力和循环地图的所有键来搜索给定实例之间的关联,但这既低效又不优雅
您知道有哪些数据结构/免费库支持此功能吗?我不想重新发明轮子
NB:这不是一个“数据库”问题。这些对象是用于实时计算的纯POJO,我不需要持久性的东西。使用AssociationClass,您可以让ClassA和ClassB都包含对AssociationClass的引用:
private AssociationClass association;
或者,另一种方法
ClassA可以包含:
private List<ClassB> classBList;
private List<ClassA> classAList;
私有列表类列表;
类B可以包含:
private List<ClassB> classBList;
private List<ClassA> classAList;
私有列表类列表;
通过实现此功能,您可以从关联的类中访问关联。可能来自的Multimap或BiMap可以满足您的需要。这看起来像是一个问题,您需要使用多个键获取数据。您希望按类别A和类别B进行搜索。这通常会导致数据顶部有多个映射,因此每个映射都会保留一个搜索键,以进入底层数据。也许这样的方法会奏效:
public class Data {
public ClassA a;
public ClassB b;
public AssociationClass association;
}
Map<ClassA, Data> aData;
Map<ClassB, Data> bData;
Map<AssociationClass, Data> associationData;
获取数据您可以查询每个地图以获得所需的数据。您甚至可以将对
类作为数据的另一个索引:
Map<Pair<ClassA, ClassB>, Data> pairData;
Map pairData;
这种方法的问题是,如果基础数据发生很大变化,则必须确保所有地图都同步。如果这主要是一个只读问题,那么您可以创建映射,然后用您的密钥查询映射到数据中。rmarimon说得对,它需要两个映射,但我认为您需要的是a-B,而不是a-data和B-data 因此,您只需要两张地图:
Hashmap bByA = new HashMap();
Hashmap aByB = new HashMap();
这给了你想要的一切,自由而简单。为什么不在每节课上放一张地图呢
class ClassA {
...
private Map<ClassB, AssociationClass> associations
= HashMap<ClassB, AssociationClass>();
...
}
class ClassA {
...
private Map<ClassA, AssociationClass> associations
= HashMap<ClassB, AssociationClass>();
...
}
A类{
...
私人地图协会
=HashMap();
...
}
甲级{
...
私人地图协会
=HashMap();
...
}
谢谢你的建议
我终于重新发明了轮子。。。
我已经编写了一个用于保持关联的通用类。
我用了两张地图,同步的
关联持有者提供以下方法
void setAssociation(LeftClass left, RightClass right, AssociationClass assoc);
AssociationClass getAssociation(LeftClass left, RightClass right);
Map<RightClass, AssociationClass> getAssocationsLeft(LeftClass left);
Map<LeftClass, AssociationClass> getAssocationsRight(RightClass right);
void removeAssociation(LeftClass left, RightClass right);
void集合关联(LeftClass left,RightClass right,AssociationClass assoc);
AssociationClass getAssociation(LeftClass left,RightClass right);
Map GetAssociationsLeft(左类左);
Map GetAssociationsRight(RightClass right);
无效删除关联(LeftClass left,RightClass right);
代码如下:
import java.util.HashMap;
/** This class holds many to many associations between two classes. */
public class AssociationHolder<LeftClass, RightClass, AssociationClass> {
// -------------------------------------------------------
// Attributes
// -------------------------------------------------------
private HashMap<LeftClass, HashMap<RightClass, AssociationClass>> associationsLeft =
new HashMap<LeftClass, HashMap<RightClass,AssociationClass>>();
private HashMap<RightClass, HashMap<LeftClass, AssociationClass>> associationsRight =
new HashMap<RightClass, HashMap<LeftClass,AssociationClass>>();
// -------------------------------------------------------
// Methods
// -------------------------------------------------------
/**
* Set an association between two instance.
* Any prior association is overwritten.
*/
public void setAssociation(LeftClass left, RightClass right, AssociationClass association) {
// Get the map for the left
HashMap<RightClass, AssociationClass> leftMap = this.associationsLeft.get(left);
// No association defined yet for this left key ? => Create new map
if (leftMap == null) {
leftMap = new HashMap<RightClass, AssociationClass>();
this.associationsLeft.put(left, leftMap);
}
// Get the map for the right
HashMap<LeftClass, AssociationClass> rightMap = this.associationsRight.get(right);
// No association defined yet for this right key ? => Create new map
if (rightMap == null) {
rightMap = new HashMap<LeftClass, AssociationClass>();
this.associationsRight.put(right, rightMap);
}
// Set the assoication on both maps
leftMap.put(right, association);
rightMap.put(left, association);
}
/** @return null if no association found. */
public AssociationClass getAssociation(LeftClass left, RightClass right) {
// Use left maps (could have used the right one as well)
HashMap<RightClass, AssociationClass> leftMap = this.associationsLeft.get(left);
if (leftMap == null) return null;
return leftMap.get(right);
}
/** Get all associations defined for a given Left instance. */
public HashMap<RightClass, AssociationClass> getAssociationsLeft(LeftClass left) {
HashMap<RightClass, AssociationClass> leftMap = this.associationsLeft.get(left);
// No map defined ? return empty one instead of null
if (leftMap == null) {
return new HashMap<RightClass, AssociationClass>();
} else {
return leftMap;
}
}
/** Get all associations defined for a given Right instance. */
public HashMap<LeftClass, AssociationClass> getAssociationsRight(RightClass right) {
HashMap<LeftClass, AssociationClass> rightMap = this.associationsRight.get(right);
// No map defined ? return empty one instead of null
if (rightMap == null) {
return new HashMap<LeftClass, AssociationClass>();
} else {
return rightMap;
}
}
/**
* Remove an association between two instances.
*/
public void removeAssociation(LeftClass left, RightClass right) {
HashMap<RightClass, AssociationClass> leftMap = this.getAssociationsLeft(left);
HashMap<LeftClass, AssociationClass> rightMap = this.getAssociationsRight(right);
leftMap.remove(right);
rightMap.remove(left);
}
}
import java.util.HashMap;
/**这个类包含两个类之间的多对多关联*/
公共类协会持有人{
// -------------------------------------------------------
//属性
// -------------------------------------------------------
私有HashMap associationsLeft=
新的HashMap();
私有HashMap associationsRight=
新的HashMap();
// -------------------------------------------------------
//方法
// -------------------------------------------------------
/**
*设置两个实例之间的关联。
*任何先前的关联都将被覆盖。
*/
公共void集合关联(LeftClass left,RightClass right,AssociationClass association){
//拿左边的地图
HashMap leftMap=this.associationsLeft.get(左);
//尚未为此左键定义关联?=>创建新映射
if(leftMap==null){
leftMap=newhashmap();
this.associationsLeft.put(left,leftMap);
}
//找到右边的地图
HashMap rightMap=this.associationsRight.get(右);
//尚未为此右键定义关联?=>创建新映射
if(rightMap==null){
rightMap=newhashmap();
this.associationsRight.put(right,rightMap);
}
//在两个映射上设置关联
leftMap.put(右,关联);
rightMap.put(左,关联);
}
/**@如果未找到关联,则返回null*/
公共关联类getAssociation(LeftClass左,RightClass右){
//使用左侧地图(也可以使用右侧地图)
HashMap leftMap=this.associationsLeft.get(左);
if(leftMap==null)返回null;
返回leftMap.get(右);
}
/**获取为给定左实例定义的所有关联*/
公共HashMap getAssociationsLeft(LeftClass left){
HashMap leftMap=this.associationsLeft.get(左);
//未定义映射?返回空映射,而不是空映射
if(leftMap==null){
返回新的HashMap();
}否则{
返回leftMap;
}
}
/**获取为给定的正确实例定义的所有关联*/
公共HashMap GetAssociationRight(RightClass right){
HashMap rightMap=this.associationsRight.get(右);
//未定义映射?返回空映射,而不是空映射
if(rightMap==null){
返回新的HashMap();
}否则{
返回rightMap;
}
}
/**
*删除两个实例之间的关联。
*/
公共vo