将列表转换为多个嵌套映射

时间:2012-08-29 16:10:17

标签: java

我得到一个像这样的List,每行一个Object包含普通字符串:

Vehicle, Name, Property, Value
------------------------------
Car, VW, Tires, 4
Car, VW, Doors, 4
Car, Porsche, Tires, 4
Car, Porsche, Doors, 2
Car, Porsche, Color, Red
Plane, A340, Tires, 12
Plane, A340, Color, White
Plane, A750, Doors, 6
Forklift, ABC345, Color, Orange
... and so on

我希望以以下形式将其作为JSON返回:

{ 
"Car" : {
     "VW":{ "Tires" : "4", "Doors": "4"},
     "Porsche":{ "Tires" : "4", "Doors": "2" }
     },
"Plane":{
     "A340":{ "Tires" : "12", "Color" : "White" },
     "A750":{ "Doors" : "6" }
     },
"Forklift" : {
     "ABC345": { "Color" : "Orange" }
     }
}

我尝试使用奇怪的

HashMap<String, HashMap<String, HashMap<String, String>>>

但说实话,我不知道如何正确设置。当我遍历列表并写入HashMap时,属性和值总是被覆盖,所以我的结果如下所示:

{ 
"Car" : {
     "Porsche":{ "Tires" : "4" }
     },
"Plane":{
     "A750":{ "Doors" : "6" }
     },
"Forklift" : {
     "ABC345": { "Color" : "Orange" }
     }
}

我是Java的新手,不知道如何处理这种嵌套的地图。

我的意思是,要求被宠爱是有点尴尬,但也许有人可以告诉我如何以正确的方式做到这一点。

编辑:这里我是如何添加值的,我没有粘贴这部分代码,因为我不知道如何检查并决定该怎么做。

HashMap<String, HashMap<String, HashMap<String, String>>> vehiclesData = new HashMap<String, HashMap<String, HashMap<String, String>>>();

for( VehicleReportRow vehicleReportRow : unpreparedRows ){
    String vehicle = vehicleReportRow.getVehicle();
    String name = vehicleReportRow.getName();
    String property = vehicleReportRow.getProperty();
    String value = vehicleReportRow.getValue();

    //the most inner dataset
    HashMap<String,String> propAndVal = new HashMap<String, String>();
   propAndVal.put(property, value);

    //the middle dataset            
    HashMap<String, HashMap<String,String>> nameToProps = new HashMap<String, HashMap<String,String>>();
    nameToProps.put(name, propAndVal);

    //and finally the outer dataset
    vehiclesData.put(vehicle, nameToProps);
}

2 个答案:

答案 0 :(得分:1)

你正在使用两个深度的hashmap,而你的数据结构是三个深度。就像Inception:“我们需要更深入!”

{ 
1: "Car" : {
2:     "VW":{ 
3:         "Tires" : "4", "Doors": "4"
       },
       "Porsche":{ 
           "Tires" : "4", "Doors": "2" 
       }
   },
}

答案 1 :(得分:1)

现在看到你的代码,问题正如我在评论中所暗示的那样。例如你每次循环创建一个新的nameToProps和一个新的propAndVal,而不是检查是否存在你应该添加的现有的for( VehicleReportRow vehicleReportRow : unpreparedRows ){ String vehicle = vehicleReportRow.getVehicle(); String name = vehicleReportRow.getName(); String property = vehicleReportRow.getProperty(); String value = vehicleReportRow.getValue(); // check if we have an outermost entry for this vehicle type and if not then // create one and store it in vehiclesData so that next time we can get the same // map for this vehicle type HashMap<String, HashMap<String,String>> nameToProps = vehiclesData.get(vehicle);; if (nameToProps == null) { nameToProps = new HashMap<String, HashMap<String,String>>(); vehiclesData.put(vehicle, nameToProps); } // similarly, check if we already have a props to values map for this name // and create and store one if not HashMap<String,String> propAndVal = nameToProps.get(name); if (propAndVal == null) { propAndVal = new HashMap<String, String>(); nameToProps.put(name, propAndVal); } // store the property and value propAndVal.put(property, value); } 。你想要一些检查,例如:

{{1}}

如果有任何需要进一步解释,请告诉我。