在JavaSpringBoot中,按id将列表数据与嵌套的对象列表分组
这是我的表格数据 产品在JavaSpringBoot中,按id将列表数据与嵌套的对象列表分组,java,spring-boot,collections,db2,java-stream,Java,Spring Boot,Collections,Db2,Java Stream,这是我的表格数据 产品 id eid item stage score url product --------------------------------------------------------------------------------------------------------- 1933337 d96f32c4decc
id eid item stage score url product
---------------------------------------------------------------------------------------------------------
1933337 d96f32c4decc7d0 host Server 0.3908 "https://" "Real"
2933337 96f32c4decc7d0 local Server 0.3908 "https://" "Virtual"
1933337 d96f32c4decc7d0 host Server 0.3908 "https://" "Real"
2933337 96f32c4decc7d0 local Server 0.3908 "https://" "Virtual"
我想通过id聚合数据,每个id包含嵌套的items列表&每个id包含java中的嵌套队列列表
预期产出:
[
{
"id": 1933337,
"eid": d96f32c4decc7d0,
"items": [
{
"item": "host",
"stage": "Server",
"score" : 0.3908,
"queued" : [
{
"url": "https://",
"product": "Virtual"
},
{
"url": "https://",
"product": "Real"
}
]
}
]
},
{
"id": 2933337,
"eid": 96f32c4decc7d0,
"items": [
{
"item": "local",
"stage": "Server",
"score" : 0.3908,
"queued" : [
{
"url": "https://",
"product": "Virtual"
},
{
"url": "https://",
"product": "Real"
}
]
}
]
}
]
我尝试过streamapi,但它覆盖了分组数据&每个列表都有相同的数据
我尝试了DB2查询:
DECLARE GLOBAL TEMPORARY TABLE prod_json_objcts AS (
WITH BASE AS (
select id, item,
JSON_OBJECT('item' value item,
'itemScore' value itemScore,
'stage' value stage,
'reco' VALUE JSON_OBJECT('product' value product,
'url' value url ,
'score' value score
FORMAT JSON )
FORMAT JSON ABSENT ON NULL
RETURNING VARCHAR(200) FORMAT JSON) ITEM_JSON
FROM PROD_T )
SELECT JSON_OBJECT ( KEY 'id' VALUE ID ,
KEY 'itens' VALUE
JSON_ARRAY ( LISTAGG( ITEM_JSON , ', ') WITHIN GROUP (ORDER BY ITEM) FORMAT JSON )
FORMAT JSON ) json_objects
FROM BASE GROUP BY ID
) WITH DATA
SELECT JSON_ARRAY (select json_objects FROM session.prod_json_objcts format json) FROM SYSIBM.SYSDUMMY1
生成json数组:
[{"id":2078012,
"itens":[
{"item":"build",
"score":-0.2585,
"stage":"",
"reco":{"product":"Notification",
"url":"https:\/\/"}},
{"item":"cos",
"score":-0.1334,
"stage":"",
"reco":{"product":"Language",
"url":"https:\/\/"}}
]
},
{"id":2100000,
"itens":[
{"item":"alpha",
"score":-0.1334,
"stage":"",
"queued":{"product":"Letter",
"url":"https:\/\/"}
},
{"item":"beta",
"score":-0.2585,
"stage":"",
"queued":{"product":"anouncement",
"url":"https:\/\/"}
]
}
]
我希望队列对象作为数组
弹簧靴:
private List<ProductDto> parseData(List<Product> productList) {
Map<Long, List<Product>> mapById =
productList.stream().collect(Collectors.groupingBy(Product::getId));
List<ProductDto> productDtos= new ArrayList<>();
for (Map.Entry<Long,List<Product>> entry : mapById.entrySet()) {
Map<String, List<Product>> mapByItem =
entry.getValue().stream().collect(Collectors.groupingBy(Product::getItem));
List<Items> itemList = new ArrayList<>();
for (Map.Entry<String,List<Product>> entryUc : mapByItem.entrySet()) {
List<Queued> qList = entryUc.getValue().stream()
.map(Product::getQ)
.collect(Collectors.toList());
itemList .add(new Items(entryUc.getValue().get(0).getItem(),
entryUc.getValue().get(0).getScore(),
entryUc.getValue().get(0).getStage(),
qList ));
}
productDtos.add(new ProductDto(
entry.getKey(),
useCasesList));
}
return productDtos;
}
私有列表解析数据(列表产品列表){
mapById=
productList.stream().collect(Collectors.groupingBy(Product::getId));
List productDtos=new ArrayList();
对于(Map.Entry:mapById.entrySet()){
mapByItem=
entry.getValue().stream().collect(Collectors.groupingBy(Product::getItem));
List itemList=new ArrayList();
对于(Map.Entry entryUc:mapByItem.entrySet()){
List qList=entryUc.getValue().stream()
.map(产品::getQ)
.collect(Collectors.toList());
itemList.add(新项目(entryUc.getValue().get(0.getItem()),
entryUc.getValue().get(0.getScore(),
entryUc.getValue().get(0.getStage(),
qList);
}
productDtos.add(新ProductDto(
entry.getKey(),
使用案例列表);
}
退货;
}
它也不起作用。
在地图上,我多次看到同一个物体。
它覆盖了entris或添加了重复数据,但不知道确切原因。该行的输出/结构是什么?Map mapById=productList.stream().collect(Collectors.groupingBy(Product::getId))您已经花费了大量精力试图解释这种情况,我们尊重这一点,但请不要发表如此冗长的问题,因此,请尽量使事情简单和简短。