【问题标题】:loop through multi map循环遍历多张地图
【发布时间】:2015-01-15 11:26:18
【问题描述】:

我有这样的多张地图TreeMap[String,List<String>]:

{DLK_1086686311826_10025=[AufAufVerbindung, Doppelkante 1, AUF_1086686287581_9999, AUF_1060589919844_59496],
DLK_1086686485796_10173=[AufAufVerbindung, Doppelkante 1, AUF_1086686329972_10049, AUF_1060589919844_59496],
DLK_1087469315294_9204=[AufAufVerbindung, Doppelkante 1, AUF_1079023138936_6682, AUF_1060589919844_59496],
DLK_1421268558017_1742=[AufAufVerbindung, Part-Of-Relation 1, AUF_1421268533080_1741, AUF_1087981634453_7022],
DLK_1421268747905_1778=[AufAufVerbindung, Part-Of-Relation 3, AUF_1421268719761_1776, AUF_1421268533080_1741],
DLK_1421268798097_1780=[AufAufVerbindung, Part-Of-Relation 2, AUF_1421268568003_1743, AUF_1087981634453_7022], 
DLK_1421272459527_1783=[AufAufVerbindung, Part-Of-Relation 4, AUF_1087981634453_7022, AUF_1421272434570_1781]}

我想循环并获得如下结果。我怎样才能遍历这个多图? :

{AUF_1060589919844_59496=[AUF_1086686287581_9999, AUF_1086686329972_10049, AUF_1079023138936_6682],
 AUF_1087981634453_7022=[AUF_1421268533080_1741,AUF_1421268568003_1743], 
 AUF_1421268533080_1741=AUF_1421268719761_1776],
 AUF_1421272434570_1781=AUF_1087981634453_7022]}

我已经编写了这段代码,但它也将我不想要的所有值添加到地图中:

    public TreeMap<String, List<String>> findCombiFunctions() {
    ArrayList<String> combiFunc = new ArrayList<String>();
    Set<String> tempThird = new HashSet<String>();
    for (Entry<String, List<String>> entryAufAuf : map_AufAufVerbindung
            .entrySet()) {
        tempThird.add(entryAufAuf.getValue().get(3));
        for (String thirdVal : tempThird) {
            if (thirdVal.equals(entryAufAuf.getValue().get(3))) {
                System.out.println(thirdVal + " "
                        + entryAufAuf.getValue().get(2));
                combiFunc.add(entryAufAuf.getValue().get(2));
                map_combiFunc.put(entryAufAuf.getValue().get(3), combiFunc);
            }
        }
    }
    return map_combiFunc;
}

【问题讨论】:

  • 地图第一行末尾好像少了一个逗号...
  • 是的,你是对的。复制过程中遗漏了它。

标签: iterator multimap


【解决方案1】:

我使用一个集合来获取每个条目中的第三个值并重置combiFunc ArrayList。

public TreeMap<String, List<String>> findCombiFunctions() {

        ArrayList<String> combiFunc = new ArrayList<String>();
        Set<String> tempThird = new HashSet<String>();
        ArrayList<String> tempThirdArray = new ArrayList<String>();
        for (Entry<String, List<String>> entryAufAuf : map_AufAufVerbindung
                .entrySet()) {
            tempThird.add(entryAufAuf.getValue().get(3));

        }
        tempThirdArray.addAll(tempThird);
        System.out.println(tempThirdArray);
        for (int i = 0; i < tempThird.size(); i++) {
            for (Entry<String, List<String>> entryAufAuf : map_AufAufVerbindung
                    .entrySet()) {

                if (entryAufAuf.getValue().get(3).equals(tempThirdArray.get(i))) {
                    System.out.println(entryAufAuf.getValue().get(2) + " "
                            + tempThirdArray.get(i));
                    combiFunc.add(entryAufAuf.getValue().get(2));
                    map_combiFunc.put(tempThirdArray.get(i), combiFunc);
                }
            }
            combiFunc = new ArrayList<String>();            
        }
        return map_combiFunc;
    }

【讨论】:

    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    相关资源
    最近更新 更多