提取数据问题

[
            {
                "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"
            },
]

这种

阅读 1.1k
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));
        }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题