我有一个带有嵌套list字段的DTO
对象列表。
其目的是按照id
字段和merge对它们进行分组,然后使用Streams API对列表进行排序。
class DTO {
private Long id;
private List<ItemDTO> items;
}
class ItemDTO {
private Long priority;
private Long value;
}
// input
List<DTO> dtoList = List.of(
DTO(1, List.of(ItemDTO(1, 1), ItemDTO(7, 2))),
DTO(2, List.of(ItemDTO(1, 1), ItemDTO(2, 2))),
DTO(1, List.of(ItemDTO(10, 3), ItemDTO(1, 4)))
);
我需要使用相同的id
字段对这些嵌套对象进行分组,并按字段priority
合并降序中的所有项。
这个dtoList
的最终结果如下所示:
// output
List<DTO> resultList = [
DTO(1, List.of(ItemDTO(10,3), ItemDTO(7,2), ItemDTO(1,1), ItemDTO(1,4)),
DTO(2, List.of(ItemDTO(2,2), ItemDTO(1,1),
];
我们能用Streams API实现这一点吗?
发布于 2022-05-17 17:09:12
我会从一个简单的分组开始,以获得一个映射Map<Long,List<DTO>>
,并在该映射的条目上进行流,并将每个映射映射到一个新的DTO。您可以提取一个方法/函数来对ItemDTO
进行排序:
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
....
Function<List<DTO>, List<ItemDTO>> func =
list -> list.stream()
.map(DTO::getItems)
.flatMap(List::stream)
.sorted(Comparator.comparing(ItemDTO::getPriority,Comparator.reverseOrder()))
.collect(Collectors.toList());
List<DTO> result =
dtoList.stream()
.collect(Collectors.groupingBy(DTO::getId))
.entrySet().stream()
.map(entry -> new DTO(entry.getKey(), func.apply(entry.getValue())))
//.sorted(Comparator.comparingLong(DTO::getId)) if the resulting list need to be sorted by id
.collect(Collectors.toList());
发布于 2022-05-17 17:00:29
您可以通过将数据按id
分组来创建中间映射,然后将每个条目转换为一个新的DTO
对象。
为此,您可以使用内置收集器groupingBy()
和flatMapping()
的组合来创建中间映射。
为了对每个id
映射的项进行排序,flatMapping()
与collectionAndThen()
一起使用。
public static void main(String[] args) {
// input
List<DTO> dtoList = List.of(
new DTO(1L, List.of(new ItemDTO(1L, 1L), new ItemDTO(7L, 2L))),
new DTO(2L, List.of(new ItemDTO(1L, 1L), new ItemDTO(2L, 2L))),
new DTO(1L, List.of(new ItemDTO(10L, 3L), new ItemDTO(1L, 4L)))
);
List<DTO> result = dtoList.stream()
.collect(Collectors.groupingBy(DTO::getId,
Collectors.collectingAndThen(
Collectors.flatMapping(dto -> dto.getItems().stream(), Collectors.toList()),
(List<ItemDTO> items) -> {
items.sort(Comparator.comparing(ItemDTO::getPriority).reversed());
return items;
})))
.entrySet().stream()
.map(entry -> new DTO(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
result.forEach(System.out::println);
}
输出
DTO{id = 1, items = [ItemDTO{10, 3}, ItemDTO{7, 2}, ItemDTO{1, 1}, ItemDTO{1, 4}]}
DTO{id = 2, items = [ItemDTO{2, 2}, ItemDTO{1, 1}]}
正如@shmosel所指出的,flatMapping()
是Java9的优点之一。您可能也会认为它是一个提醒,也许是时候转向Java9提供的模块化系统和其他有用的特性了。
完全符合Java 8的版本如下所示:
List<DTO> result = dtoList.stream()
.collect(Collectors.groupingBy(DTO::getId,
Collectors.collectingAndThen(
Collectors.mapping(DTO::getItems, Collectors.toList()),
(List<List<ItemDTO>> items) ->
items.stream().flatMap(List::stream)
.sorted(Comparator.comparing(ItemDTO::getPriority).reversed())
.collect(Collectors.toList())
)))
.entrySet().stream()
.map(entry -> new DTO(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
发布于 2022-05-17 17:32:55
这是最简单的方法。我假定您已经为您的类定义了适当的getter。
。
List<DTO> results = new ArrayList<>(dtoList.stream().collect(
Collectors.toMap(DTO::getId, dto -> dto, (a, b) -> {
a.getItems().addAll(b.getItems());
return a;
})).values());
然后根据您的需求对它们进行排序。这不需要更多的时间来做它的溪流构造,但在我看来,是不那么杂乱。
for (DTO d: results) {
d.getItems().sort(Comparator.comparing(ItemDTO::getPriority)
.reversed());
}
results.forEach(System.out::println);
打印(对这两个类使用简单的toString
)
DTO[1, [ItemDTO[10, 3], ItemDTO[7, 2], ItemDTO[1, 1], ItemDTO[1, 4]]]
DTO[2, [ItemDTO[2, 2], ItemDTO[1, 1]]]
注意:List.of
是不可变的,所以您不能更改它们。我会在您的列表构造中使用new ArrayList<>(List.of(...))
。
https://stackoverflow.com/questions/72277722
复制相似问题