如何创建字符串到从多个数据行构建的复杂对象的Java映射

如何创建字符串到从多个数据行构建的复杂对象的Java映射,java,stream,swagger,Java,Stream,Swagger,我运行一个查询并从数据库中获取以下数据 painter_id color_id color used 1 100 blue t 1 101 green t 1 102 red f 1 103 black f 1 104 yellow f 2 110

我运行一个查询并从数据库中获取以下数据

painter_id  color_id    color   used 
1           100         blue    t
1           101         green   t
1           102         red     f
1           103         black   f
1           104         yellow  f
2           110         violet  t
2           111         cyan    t
2           112         brown   t
2           113         white   f
2           114         orange  f
我需要帮助建立以下输出

{
    "colors": {
        "1": {
            "used": {
                "results": [
                    "blue",
                    "green"
                ]
            },
            "notUsed": {
                "results": [
                    "red",
                    "black",
                    "yellow"
                ]
            }
        },
        "2": {
            "used": {
                "results": [
                    "violet",
                    "cyan",
                    "brown"
                ]
            },
            "notUsed": {
                "results": [
                    "white",
                    "orange"
                ]
            }
        }
    }
}
输出的招摇过市定义为:

ColorResponse:
  type: object
  properties:
    colors:
      type: object
      additionalProperties:
        $ref: "#/definitions/ColorUsages"

ColorUsages:
  type: object
  properties:
    used:
      $ref: "#/definitions/ColorUsage"
    notUsed:
      $ref: "#/definitions/ColorUsage"

ColorUsage:
  type:  object
  properties:
    count:
      type: integer
      format: int32
      description: How many colors are there
    results:
      type: array
      items:
        type: string
        description: The color name     
在哪里

Map<String, ColorUsage> colors = new HashMap<String, ColorUsages>();
ColorUsage used;
ColorUsage notUsed;
Integer count;
List<String> results = new ArrayList<>();
还假设您可以使用以下命令:

Map<String, ColorUsages> colors = new HashMap<>();

ColorUsage.Builder usedColorUsage = ColorUsage.builder();
ColorUsage.Builder notUsedColorUsage = ColorUsage.builder();

usedColorUsage.addResultsItem(row.color());

ColorUsages.Builder colorUsagesBuilder = ColorUsages.builder();

ColorUsages colorUsages = colorUsagesBuilder
                        .used(usedColorUsage.build())
                        .notUsed(notUsedColorUsage.build())
                        .build();

colors.put(row.painter_id(), colorUsages);
Map colors=newhashmap();
ColorUsage.Builder usedColorUsage=ColorUsage.Builder();
ColorUsage.Builder notUsedColorUsage=ColorUsage.Builder();
usedColorUsage.addResultsItem(row.color());
ColorUsages.Builder colorUsagesBuilder=ColorUsages.Builder();
ColorUsages ColorUsages=colorUsagesBuilder
.used(usedColorUsage.build())
.notUsed(notUsedColorUsage.build())
.build();
colors.put(row.painter\u id(),colorUsages);

我目前获取数据,对其进行一些小的更新,并将其存储在树型表中。然后,我遍历颜色,需要为画家构建usedColorUsage和notUsedColorUsage对象。然后我需要将usedColorUsage和notUsedColorUsage添加到ColorUsage中。最后,我需要将painter_id和colorUsages的映射组合添加到颜色中。

将org.json.simple.JSONArray和org.json.simple.JSONObject与映射一起使用,以简化工作

public JSONObject buildOutPut(List<TableData> dataList) {

        JSONObject finalObject = new JSONObject();

        Map<String, JSONObject> map = new HashMap<String, JSONObject>();

        for(TableData oneRow: dataList) {
            if(map.containsKey(oneRow.getPainterId())) {

                JSONObject subObject =  map.get(oneRow.getPainterId());


                if(oneRow.getUsed().equalIgnoreCase("t")) {
                    JSONObject usedResultsObj  = (JSONObject) subObject.get("used");
                    JSONArray usedResults  = (JSONArray) usedResultsObj.get("results");
                    usedResults.add(oneRow.getColor());

                    usedResultsObj.put("results", usedResults);
                    subObject.put("used", usedResultsObj);
                    map.put(oneRow.getPainterId(), subObject);

                } else if(oneRow.getUsed().equalIgnoreCase("f")) {
                    JSONObject notUsedResultsObj  = (JSONObject) subObject.get("notUsed");
                    JSONArray notUsedResults  = (JSONArray) notUsedResultsObj.get("results");
                    notUsedResults.add(oneRow.getColor());

                    notUsedResultsObj.put("results", notUsedResults);
                    subObject.put("notUsed", notUsedResults);
                    map.put(oneRow.getPainterId(), subObject);

                }


            } else {
                JSONObject subObject = new JSONObject();

                JSONObject usedResultsObj = new JSONObject();
                JSONObject notUsedResultsObj = new JSONObject();

                JSONArray usedResults = new JSONArray();
                JSONArray notUsedResults = new JSONArray();

                if(oneRow.getUsed().equalIgnoreCase("t")) {
                    usedResults.add(oneRow.getColor());
                } else if(oneRow.getUsed().equalIgnoreCase("f")) {
                    notUsedResults.add(oneRow.getColor());
                }

                usedResultsObj.put("results", usedResults);
                notUsedResultsObj.put("results", notUsedResults);
                subObject.put("used", usedResultsObj);
                subObject.put("notUsed", notUsedResultsObj);

                map.put(oneRow.getPainterId(), subObject);
            }
        }

        finalObject.put("colors", map);

        return finalObject;

    }
公共JSONObject构建输出(列表数据列表){ JSONObject finalObject=新的JSONObject(); Map Map=newhashmap(); 对于(TableData oneRow:dataList){ if(map.containsKey(oneRow.getPainterId()){ JSONObject subObject=map.get(oneRow.getPainterId()); if(oneRow.getUsed().equalIgnoreCase(“t”)){ JSONObject usedResultsObj=(JSONObject)subObject.get(“used”); JSONArray usedResults=(JSONArray)usedResultsObj.get(“结果”); usedResults.add(oneRow.getColor()); usedResultsObj.put(“结果”,usedResults); 子对象put(“已使用”,usedResultsObj); put(oneRow.getPainterId(),子对象); }else if(oneRow.getUsed().equalIgnoreCase(“f”)){ JSONObject notUsedResultsObj=(JSONObject)subObject.get(“notUsed”); JSONArray notUsedResults=(JSONArray)notUsedResultsObj.get(“结果”); 添加(oneRow.getColor()); notUsedResultsObj.put(“结果”,notUsedResults); 子对象put(“未使用”,未使用的结果); put(oneRow.getPainterId(),子对象); } }否则{ JSONObject子对象=新的JSONObject(); JSONObject usedResultsObj=新的JSONObject(); JSONObject notUsedResultsObj=新JSONObject(); JSONArray usedResults=新的JSONArray(); JSONArray notUsedResults=新的JSONArray(); if(oneRow.getUsed().equalIgnoreCase(“t”)){ usedResults.add(oneRow.getColor()); }else if(oneRow.getUsed().equalIgnoreCase(“f”)){ 添加(oneRow.getColor()); } usedResultsObj.put(“结果”,usedResults); notUsedResultsObj.put(“结果”,notUsedResults); 子对象put(“已使用”,usedResultsObj); 子对象put(“未使用”,未使用结果); put(oneRow.getPainterId(),子对象); } } 最终对象。放置(“颜色”,地图); 返回最终对象; }
public JSONObject buildOutPut(List<TableData> dataList) {

        JSONObject finalObject = new JSONObject();

        Map<String, JSONObject> map = new HashMap<String, JSONObject>();

        for(TableData oneRow: dataList) {
            if(map.containsKey(oneRow.getPainterId())) {

                JSONObject subObject =  map.get(oneRow.getPainterId());


                if(oneRow.getUsed().equalIgnoreCase("t")) {
                    JSONObject usedResultsObj  = (JSONObject) subObject.get("used");
                    JSONArray usedResults  = (JSONArray) usedResultsObj.get("results");
                    usedResults.add(oneRow.getColor());

                    usedResultsObj.put("results", usedResults);
                    subObject.put("used", usedResultsObj);
                    map.put(oneRow.getPainterId(), subObject);

                } else if(oneRow.getUsed().equalIgnoreCase("f")) {
                    JSONObject notUsedResultsObj  = (JSONObject) subObject.get("notUsed");
                    JSONArray notUsedResults  = (JSONArray) notUsedResultsObj.get("results");
                    notUsedResults.add(oneRow.getColor());

                    notUsedResultsObj.put("results", notUsedResults);
                    subObject.put("notUsed", notUsedResults);
                    map.put(oneRow.getPainterId(), subObject);

                }


            } else {
                JSONObject subObject = new JSONObject();

                JSONObject usedResultsObj = new JSONObject();
                JSONObject notUsedResultsObj = new JSONObject();

                JSONArray usedResults = new JSONArray();
                JSONArray notUsedResults = new JSONArray();

                if(oneRow.getUsed().equalIgnoreCase("t")) {
                    usedResults.add(oneRow.getColor());
                } else if(oneRow.getUsed().equalIgnoreCase("f")) {
                    notUsedResults.add(oneRow.getColor());
                }

                usedResultsObj.put("results", usedResults);
                notUsedResultsObj.put("results", notUsedResults);
                subObject.put("used", usedResultsObj);
                subObject.put("notUsed", notUsedResultsObj);

                map.put(oneRow.getPainterId(), subObject);
            }
        }

        finalObject.put("colors", map);

        return finalObject;

    }