从元组列表中准备嵌套的hashmap

时间:2015-08-02 07:49:43

标签: java hashmap tuples

我的地图将是:

{"hashMapList":[{"aGasConsum":1,"invAmt":54.0,"invNo":"222222222","custCode":"11111111111111",
                 "customer":{
                      {"cusName": "abc"},
                      {"billAddr":"qqq"},
                      {"area":
                               {"areName":"xyz"},
                               {"id":2}
                      }
                  },
                {"aGasConsum":2,"invAmt":55.0,"invNo":"222222222","custCode":"11111111111111",
                  "customer":{
                      {"cusName": "abc"},
                      {"billAddr":"qqq"},
                      {"area":
                               {"areName":"xyz"},
                               {"id":2}
                      }
                }]}

但目前的情况是:当它获得相同的密钥时,它会与值重叠并放置。

{"hashMapList":[{"aGasConsum":1,"invAmt":54.0,"invNo":"222222222","custCode":"11111111111111",
                     "customer":{"area":{"id":2}}}, 
               {"aGasConsum":2,"invAmt":55.0,"invNo":"222222222","custCode":"11111111111111", 
                     "customer":{"area":{"id":2}}}]}

我试过的代码:

public List<HashMap> displayHashMap(List<Tuple> tupleList, HashMap container) {
    HashMap map = new HashMap();
    List<HashMap> listHash = new ArrayList<HashMap>();

    Map<String, List<HashMap>> exist = new HashMap<String, List<HashMap>>();
    List<HashMap> existMap = new ArrayList<HashMap>();
    for (int i = 0; i < tupleList.size(); i++) {
        for (String t : (List<String>) container.get("columns")) {
            if (t.indexOf('.', t.indexOf('.') + 1) != -1) {
                String[] parts = t.split("\\.");
                HashMap m1 = new HashMap();
                m1.put(parts[2], tupleList.get(i).get(parts[2]));
                HashMap m2 = new HashMap();
                m2.put(parts[1], m1);
                map.put(parts[0], m2);

                //existMap.add(m1);
                existMap.add(m2);
                exist.put(parts[0], existMap);

            } else if (t.contains(".")) {
                String[] parts = t.split("\\.");
                HashMap m3 = new HashMap();
                m3.put(parts[1], tupleList.get(i).get(parts[1]));
                map.put(parts[0], m3);

                existMap.add(m3);
                exist.put(parts[0], existMap);

            } else {
                map.put(t, tupleList.get(i).get(t));
            }
        }
        listHash.add(map);
        map = new HashMap();
    }
    System.err.println("final: " + existMap);
    System.err.println("final: " + exist);
    return listHash;
}

我很困惑如何处理这个循环并制作一个嵌套的hashmap。

1 个答案:

答案 0 :(得分:0)

    public List<HashMap> displayHashMap(List<Tuple> tupleList, HashMap container) {
    HashMap map = new HashMap();
    List<HashMap> listHash = new ArrayList<HashMap>();

    HashMap existMap = new HashMap();
    HashMap existMap1 = new HashMap();
    List<HashMap> existMapList = new ArrayList<HashMap>();
    for (int i = 0; i < tupleList.size(); i++) {
        for (String t : (List<String>) container.get("columns")) {
            if (t.indexOf('.', t.indexOf('.') + 1) != -1) {
                String[] parts = t.split("\\.");
                existMap1.put(parts[2], tupleList.get(i).get(parts[2]));
                if (!existMap.containsKey(parts[1])) {
                    existMap.put(parts[1], existMap1);
                }
                existMapList.add(existMap);
                if (map.containsKey(parts[0])) {
                    map.put(parts[0], existMapList);
                }
            } else if (t.contains(".")) {
                String[] parts = t.split("\\.");
                existMap.put(parts[1], tupleList.get(i).get(parts[1]));
                map.put(parts[0], existMap);
            } else {
                map.put(t, tupleList.get(i).get(t));
            }
            existMapList = new ArrayList<HashMap>();
        }
        existMapList = new ArrayList<HashMap>();
        listHash.add(map);
        existMap = new HashMap();
        existMap1 = new HashMap();
        map = new HashMap();
    }
    return listHash;
}

完成!!