在列表中排列数据

Arrange data in a List

我有一个存储 ID 和日期的列表。 ID在系统中可以重复。 我需要将日期添加到已经存在的 ID 中。

List<Logs> newList = Stream.of (firstFileLog, second)
.flatMap (Collection::stream)
.collect (Collectors.toList ());
System.out.println (newList);

input data-> 
[{id='1', time=15.01.2021}
, {id='2', time=12.05.2021}
, {id='3', time=14.02.2021}
, {id='4', time=11.05.2022}
, {id='1', time=30.09.2012}
, {id='2', time=02.01.2021}
, {id='1', time=18.02.2024}
]

我想要这个

   > output data -> [{id='1', time=15.01.2021, time=30.09.2012,
    > time=18.02.2024} , {id='2', time=12.05.2021, time=02.01.2021} ,
    > {id='3', time=14.02.2021} , {id='4', time=11.05.2022} ]

有办法实现吗?

您可以按 id 地图分组到 time 并收集要列出的值。假设您有 idtime 的适当吸气剂:

Stream.of(firstFileLog, second)
        .flatMap (Collection::stream)
        .collect(Collectors.groupingBy(Logs::getId,
                 Collectors.mapping(Logs::getTime,Collectors.toList())))
        .entrySet().forEach(System.out::println);

得到如下输出:

1=[15.01.2021, 30.09.2012, 18.02.2024]
2=[12.05.2021, 02.01.2021]
3=[14.02.2021]
4=[11.05.2022]