如何使用流将 Collectors.groupingby 收集器创建的列表的元素映射到其他类型?
How can I map the elements of the Lists created by the Collectors.groupingby collector to an other type using streams?
我有一个具有多个属性的对象流,我想为这些对象的两个属性创建一个映射。
例如:
class MyClass
{
private int A;
private int B;
private String C;
//constructor, getters, setters, etc...
}
使用 Collectors.groupingBy(),我可以创建一个 Map>:
Map<String, List<Myclass>> = inputStream.collect(Collectors.groupingby(MyClass::getC));
但我想要一个以 Myclass.C 作为键并以 MyClass.B 作为列表元素的 Map< String, List< int>>。
MyClass.A 不应包含在结果中。
我该怎么做?
提前致谢
inputStream.collect(
Collectors.groupingBy(
MyClass::getC,
Collectors.mapping(
MyClass::getB,
Collectors.toList())));
我有一个具有多个属性的对象流,我想为这些对象的两个属性创建一个映射。
例如:
class MyClass
{
private int A;
private int B;
private String C;
//constructor, getters, setters, etc...
}
使用 Collectors.groupingBy(),我可以创建一个 Map
Map<String, List<Myclass>> = inputStream.collect(Collectors.groupingby(MyClass::getC));
但我想要一个以 Myclass.C 作为键并以 MyClass.B 作为列表元素的 Map< String, List< int>>。
MyClass.A 不应包含在结果中。
我该怎么做?
提前致谢
inputStream.collect(
Collectors.groupingBy(
MyClass::getC,
Collectors.mapping(
MyClass::getB,
Collectors.toList())));