groupingBy 后将对象列表从类型 A 转换为类型 B
After groupingBy convert list of objects from Type A to Type B
Map<Integer,List<ItemTypeA>> list = data.stream().collect(groupingBy(ItemTypeA::getId));
我有一个将 ItemTypeA 转换为 ItemTypeB 的函数。
public ItemTypeB convert (ItemTypeA);
这里groupingBy
后如何使用,最终结果如下图。
Map<Integer,List<ItemTypeB>> map = data.stream().collect(groupingBy(ItemTypeA::getId),
如何调用函数将ItemTypeA
转换为ItemTypeB
?;
您可以使用 Collectors.mapping
:
Map<Integer,List<ItemTypeB>> output =
data.stream()
.collect(Collectors.groupingBy(ItemTypeA::getId,
Collectors.mapping(a->convert(a),
Collectors.toList())));
Map<Integer,List<ItemTypeA>> list = data.stream().collect(groupingBy(ItemTypeA::getId));
我有一个将 ItemTypeA 转换为 ItemTypeB 的函数。
public ItemTypeB convert (ItemTypeA);
这里groupingBy
后如何使用,最终结果如下图。
Map<Integer,List<ItemTypeB>> map = data.stream().collect(groupingBy(ItemTypeA::getId),
如何调用函数将ItemTypeA
转换为ItemTypeB
?;
您可以使用 Collectors.mapping
:
Map<Integer,List<ItemTypeB>> output =
data.stream()
.collect(Collectors.groupingBy(ItemTypeA::getId,
Collectors.mapping(a->convert(a),
Collectors.toList())));