栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java多对多关联图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java多对多关联图

感谢您的建议。

我终于重新发明了轮子……我编写了一个用于持有关联的通用类。我使用两张同步的地图。

协会持有人提供以下方法

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);

这是代码:

import java.util.HashMap;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    // -------------------------------------------------------        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);    }        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);    }        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;        }       }        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;        }       }        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);      }}

我希望这可以在将来对某人有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404337.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号