代码之家  ›  专栏  ›  技术社区  ›  Jeeppp

使用streams从Json创建嵌套映射

  •  2
  • Jeeppp  · 技术社区  · 4 年前

    我将下面的Json读入具有相同结构的嵌套pojo中。

    {
      "employees": [
        {
          "name": "John",
          "age": "30",
          "proData": [
            {
              "year": "1",
              "idList": [
                "234342",
                "532542",
                "325424",
                "234234"
              ]
            },
            {
              "year": "2",
              "idList": [
                "234342",
                "532542",
                "325424",
                "234234"
              ]
            },
            {
              "year": "3",
              "idList": [
                "234342",
                "532542",
                "325424",
                "234234"
              ]
            }
          ]
        },
        {
          "name": "Scott",
          "age": "32",
          "proData": [
            {
              "year": "1",
              "idList": [
                "234342",
                "532542",
                "325424",
                "234234"
              ]
            },
            {
              "year": "2",
              "idList": [
                "234342",
                "532542",
                "325424",
                "234234"
              ]
            },
            {
              "year": "3",
              "idList": [
                "234342",
                "532542",
                "325424",
                "234234"
              ]
            }
          ]
        }
      ]
    }
    

    现在我想把它映射到下面这样的结构 ProData 可以使用 idList .

    Map<String,Map<String,List<ProData>>> finalMap

    我已经写了下面这样的东西,它的作品。

            Map<String,Map<String,List<ProData>>> finalMap = new HashMap<>();
    
            for(Employee employee:root.getEmployees()){
                Map<String,List<ProData>> proDataMap = new HashMap<>();
                for(ProData proData: employee.getProData()){
                    List<ProData> finalObjs = new ArrayList<>();
                    for(String id:proData.getIdList()){
                       finalObjs.add(new ProData(id));
                    }
    
                    proDataMap.put(proData.getYear(),finalObjs);
                }
                finalMap.put(employee.getName(),proDataMap);
            }
    

    我想用streamapi做一个更好的版本。

    2 回复  |  直到 4 年前
        1
  •  2
  •   Sweeper    4 年前

    最终的结果是一个地图,所以使用 toMap 收藏家。映射的键是员工姓名(假设没有重复),映射值需要更多的工作。

    root.getEmployees().stream().collect(
        Collectors.toMap(
            Employee::getName,
            Employee::getProDataMap
        )
    }
    

    现在让我们试着写作 getProDataMap 在里面 Employee . 我们再次使用 托马普 收藏家。键是年份(假设没有重复),值是映射到的id列表 ProData 使用构造函数。

    public Map<String, List<ProData>> getProDataMap() {
        return this.getProData().stream().collect(
            Collectors.toMap(
                ProData::getYear,
                proData -> proData.getIdList().stream()
                    .map(ProData::new)
                    .collect(Collectors.toList())
            )
        )
    }
    
        2
  •  0
  •   Hard Worker    4 年前

    应该类似于,

    root.stream().forEach(employee -> {
                    Map<String,List<ProData>> proDataMap = new HashMap<>();
                    employee.getProdData().stream().forEach(data -> {
                        List<ProData> finalObjs = new ArrayList<>();
                        data.getIdList().stream().forEach(data -> {
                            finalObjs.add(new ProData(id));
                        });
                    });
                });
    

    但不管怎样,您也可以使用Gson来解析它 https://mkyong.com/java/how-to-parse-json-with-gson/