Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:创建地图列表_Java_Dictionary_Collections - Fatal编程技术网

Java:创建地图列表

Java:创建地图列表,java,dictionary,collections,Java,Dictionary,Collections,我有地图列表,地图包含json对象。。。我已经为每个json对象创建了单独的列表,这些对象具有相同的服务名称,这意味着在下面的示例中,如果列表大小应该等于serviceNameList,我希望为它创建单独的列表 意味着理想的场景应该是 List<List<Map<String, String>>> lst =new ArrayList<ArrayList<Map<String, String>>>(); List<M

我有地图列表,地图包含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中可以看出,我想要的列表大小为2,因为有两个服务名ERSampleService1和MyService

ans此列表应包含覆盖JSON数据的映射列表

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

我知道这个问题可能令人困惑

但是我被困在这里

有人知道怎么做吗

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


非常感谢您的帮助

您首先要创建最外层的列表。 然后,对于每个服务信息,您将在外部列表中添加一个列表。 对于该服务信息的每个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 :)

谢谢分享。你的问题是什么?
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 :)