提取数据问题

[

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 1,

"par": 5,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 1,

"par": 4,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 3,

"par": 3,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 4,

"par": 4,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 4,

"par": 4,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 6,

"par": 5,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 7,

"par": 3,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 8,

"par": 4,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 8,

"par": 4,

"sort": "1",

"position": "1"

}

],

如图,我怎么把相同holeIndex的值取出来放到新的集合中
变成

[

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 1,

"par": 5,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 1,

"par": 4,

"sort": "1",

"position": "1"

},

],

[

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 2,

"par": 5,

"sort": "1",

"position": "1"

},

{

"id": 367,

"playTime": "2022-06-07T03:51:20.000+00:00",

"holeIndex": 2,

"par": 4,

"sort": "1",

"position": "1"

},

]

这种


回答:

将相同属性值取出来放进新的集合,类似这种场景都可以使用stream中的groupingBy来进行分组处理,代码如下

List<Map> mapList = JSON.parseObject(json字符串, List.class);

Map<Integer, List<Map>> result = mapList.stream()

.collect(Collectors.groupingBy(map -> (Integer) map.get("holeIndex")));

for (List<Map> list : result.values()) {

System.out.println(JSON.toJSONString(list));

}

以上是 提取数据问题 的全部内容, 来源链接: utcz.com/p/944509.html

回到顶部