gpt4 book ai didi

java - 如何使用 Java lambdas 收集新类型列表中的元素?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:48:23 24 4
gpt4 key购买 nike

我一直在努力阅读 javadoc 以确定如何使用 lambda 优雅地将一种类型的行列表组合成另一种类型的分组列表。

我已经知道如何使用 Collectors.groupingBy将数据放入 Map<String, List<String>> 的语法但由于结果将在以后的各种函数调用中使用...理想情况下,我希望将这些简化为包含新映射的对象列表。

这是我的数据类型,RowData是来源...我想将数据合并到 CodesToBrands 的列表中:

class RowData {
private String id;
private String name;

public RowData() {

}

public RowData(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class CodeToBrands {
private String code;
private List<String> brands = new ArrayList<>();

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public List<String> getBrands() {
return brands;
}

public void addBrands(List<String> brands) {
this.brands.addAll(brands);
}

public void addBrand(String brand) {
this.brands.add(brand);
}
}

这是我正在编写的测试,试图找出答案......

@Test
public void testMappingRows() {
List<RowData> rows = new ArrayList<>();
rows.add(new RowData("A", "Foo"));
rows.add(new RowData("B", "Foo"));
rows.add(new RowData("A", "Bar"));
rows.add(new RowData("B", "Zoo"));
rows.add(new RowData("C", "Elf"));

// Groups a list of elements to a Map<String, List<String>>
System.out.println("\nMapping the codes to a list of brands");
Map<String, List<String>> result = rows.stream()
.collect(Collectors.groupingBy(RowData::getId, Collectors.mapping(RowData::getName, Collectors.toList())));

// Show results are grouped nicely
result.entrySet().forEach((entry) -> {
System.out.println("Key: " + entry.getKey());
entry.getValue().forEach((value) -> System.out.println("..Value: " + value));
});

/**Prints:
* Mapping the codes to a list of brands
Key: A
..Value: Foo
..Value: Bar
Key: B
..Value: Foo
..Value: Zoo
Key: C
..Value: Elf*/

// How to get these as a List<CodeToBrand> objects where each CodeToBrand objects to avoid working with a Map<String, List<String>>?
List<CodeToBrands> resultsAsNewType;
}

任何人都可以提供任何帮助来尝试以更易于使用的数据类型获得相同的总体结果吗?

提前致谢

最佳答案

您可以使用 Collectors.toMap 一次性完成:

Collection<CodeToBrands> values = rows.stream()
.collect(Collectors.toMap(
RowData::getId,
rowData -> {
CodeToBrands codeToBrands = new CodeToBrands();
codeToBrands.setCode(rowData.getId());
codeToBrands.addBrand(row.getName());
return codeToBrands;
},
(left, right) -> {
left.addBrands(right.getBrands());
return left;
}))
.values();

然后,如果您需要一个 List 而不是 Collection,只需执行以下操作:

List<CodeToBrands> result = new ArrayList<>(values);

如果您在 CodeToBrands 类中有特定的构造函数和合并方法,则可以简化上面的代码:

public CodeToBrands(String code, String brand) {
this.code = code;
this.brands.add(brand);
}

public CodeToBrands merge(CodeToBrands another) {
this.brands.addAll(another.getBrands());
return this;
}

然后,简单地做:

Collection<CodeToBrands> values = rows.stream()
.collect(Collectors.toMap(
RowData::getId,
rowData -> new CodeToBrands(rowData.getId(), rowData.getName()),
CodeToBrands::merge))
.values();

关于java - 如何使用 Java lambdas 收集新类型列表中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49392939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com