【问题标题】:Java: create List of List of MapsJava:创建地图列表列表
【发布时间】:2020-02-06 13:10:42
【问题描述】:

我有地图列表并且地图包含 json 对象...我已经为每个具有相同服务名称的 json 对象创建了单独的列表,这意味着在下面的示例中,如果列表大小应该等于 serviceNameList 并且我想创建单独的列出来

表示理想的场景应该是

List<List<Map<String, String>>> lst =new ArrayList<ArrayList<Map<String, String>>>();

List<Map<String, String>> serviceInfoList = new ArrayList<Map<String, String>()
ArrayList<String> serviceNameList = new ArrayList<String>(); 
for (int i = 0; i < serviceInfoList.size(); i++) {
            Map<String, String> serviceInfoMp = new HashMap<String, String>();
            serviceInfoMp = serviceInfoList.get(i);

            Set<Map.Entry<String, String>> serviceInfoSet = serviceInfoMp.entrySet();

            for (Map.Entry<String, String> mapentry : serviceInfoSet) {
              if (mapentry.getKey().equals("serviceName")) {
                    serviceNameList.add(mapentry.getValue());
                }
             }
        }  

地图包含 JSON 数据

表示为json下面的每个服务名称表单创建单独的列表并将其存储在列表中

{
  "ServiceData": {
    "ServiceInfo": [     
      {
        "trav": "20200131T173017Z",
        "real": "MyService",
        "lintruntime": "7",
        "upload": "184",
        "build": "1717",
        "buildproductruntime": "1709",
        "EXITCODE": "0",
        "totaltime": "3610"
      },
 {
        "trav": "20200131T173017Z",
        "real": "MyService",
        "lintruntime": "7",
        "upload": "184",
        "build": "1717",
        "buildproductruntime": "1709",
        "EXITCODE": "0",
        "totaltime": "3610"
      },
      {
        "trav": "20200131T173024Z",
        "real": "ERSampleService1",
        "lintruntime": "38",
        "upload": "381",
        "build": "1765",
        "buildproductruntime": "1767",
        "EXITCODE": "0",
        "totaltime": "3913"
      }
    ]
  }
} 

表示从上面的 Json 我想要的列表大小为 2,因为两个服务名称 ERSampleService1 和“MyService”

这个列表应该包含覆盖 JSON 数据的地图列表。

基本上我想为每个服务信息创建列表..

我知道问题可能令人困惑

但我被困在这里......

谁知道怎么做

简而言之,创建地图列表并遍历它,

感谢任何帮助

【问题讨论】:

  • 感谢分享。你有什么问题?

标签: java dictionary collections


【解决方案1】:

您首先要创建最外层的列表。 然后,对于每个服务信息,您将向该外部列表添加一个列表。 对于该服务信息的每个 json 对象,您将创建一个映射,然后将该映射放入内部列表中。你最终会得到类似的东西。

List<List<Map<String, String>>> outerList = new ArrayList<>();
    for(ServiceInfo so : serviceInfos){
        List<Map<String, String>> innerList = new ArrayList<>();
        for(JsonObject j : getJsonObjectsForThisServiceInfo(so)){
            Map<String, String> map = new HashMap<>();
            // Fill in the map with what you need
            // ...
            innerList.add(map);
        }
        outerList.add(innerList);

    }
//Use your new list after here for whatever you want :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2020-11-05
    • 2013-09-03
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多