Java 按枚举类型列表对流进行分组
我有一个产品类别:Java 按枚举类型列表对流进行分组,java,java-stream,grouping,Java,Java Stream,Grouping,我有一个产品类别: class Product { String name; List<Group> group; //more fields, getters, setters public Product(String name, Group... group) { this.name = name; this.group = Arrays.asList(group); } } 从产品列表中,我想创建一个M
class Product {
String name;
List<Group> group;
//more fields, getters, setters
public Product(String name, Group... group) {
this.name = name;
this.group = Arrays.asList(group);
}
}
从产品列表中,我想创建一个Map
输入示例:
List<Product> productList = new ArrayList<>();
productList.add(new Product("A", Group.BABY, Group.MATERNITY));
productList.add(new Product("B", Group.BABY, Group.LEISURE, Group.SPORT));
productList.add(new Product("C", Group.SPORT, Group.LEISURE));
productList.add(new Product("D", Group.LEISURE, Group.SPORT, Group.FORMALATTIRE));
productList.add(new Product("E", Group.SPORT, Group.LEISURE));
productList.add(new Product("F", Group.FORMALATTIRE, Group.LEISURE));
如何使用列表
预期结果如下所示,其中对于productList中存在的每个组,映射到在其字段group
{MATERNITY=[A], FORMALATTIRE=[D, F], LEISURE=[B, C, D, E, F], SPORT=[B, C, D, E], BABY=[A, B]}
您可以使用
flatMap
创建Entry
流,然后使用收集器将它们收集到Map
中。mapping
productList.stream()
.flatMap(p->p.getGroup()
.stream()
.map(g->new AbstractMap.SimpleEntry<>(g,p.getName()))) // or from jdk 9 you can use Map.entry(g, p.getName());
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
productList.stream()
.flatMap(p->p.getGroup()
.stream()
.map(g->newAbstractMap.SimpleEntry(g,p.getName())//或者从jdk 9中可以使用map.entry(g,p.getName());
.collect(Collectors.groupingBy(Map.Entry::getKey、,
Collectors.mapping(Map.Entry::getValue,Collectors.toList());
您可以flatMap
将每个产品中的组
映射到产品的名称
,然后通过组
将相应的名称
映射为值对其进行分组。例如:
Map<Group, List<String>> groupToNameMapping = productList.stream()
.flatMap(product -> product.getGroup().stream()
.map(group -> new AbstractMap.SimpleEntry<>(group, product.getName())))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
Map groupToNameMapping=productList.stream()
.flatMap(product->product.getGroup().stream())
.map(组->新建AbstractMap.SimpleEntry(组,product.getName()))
.collect(Collectors.groupingBy(Map.Entry::getKey、,
Collectors.mapping(Map.Entry::getValue,Collectors.toList());
或者,要获得组到产品列表的映射,您可以使用以下公式:
Map<Group, List<Product>> groupToProductMapping = productList.stream()
.flatMap(product -> product.getGroup().stream()
.map(group -> new AbstractMap.SimpleEntry<>(group, product)))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
Map groupToProductMapping=productList.stream()
.flatMap(product->product.getGroup().stream())
.map(组->新建AbstractMap.SimpleEntry(组、产品)))
.collect(Collectors.groupingBy(Map.Entry::getKey、,
Collectors.mapping(Map.Entry::getValue,Collectors.toList());
条目的类型也是可推断的,因此可以使用AbstractMap。SimpleEntry
将您的方法捆绑在一起,但获取错误不兼容的类型:推理变量U的下界不兼容:乘积,T#2,对象下界:字符串
没关系。是我的错。我期待着Map
。我已将p.getName更改为p,它正在按设计工作。尝试过您的方法,但获得了错误不兼容的类型:推理变量U的下界不兼容:乘积,T#2,对象下界:String@nopens编辑了答案。它将解决问题,并提供您所需要的。非常感谢。第二个答案正是我想要的。
Map<Group, List<String>> groupToNameMapping = productList.stream()
.flatMap(product -> product.getGroup().stream()
.map(group -> new AbstractMap.SimpleEntry<>(group, product.getName())))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
Map<Group, List<Product>> groupToProductMapping = productList.stream()
.flatMap(product -> product.getGroup().stream()
.map(group -> new AbstractMap.SimpleEntry<>(group, product)))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));