gpt4 book ai didi

java - 根据结果​​数据组合两个列表以在 java 8 中创建一个新列表

转载 作者:行者123 更新时间:2023-12-01 17:50:08 25 4
gpt4 key购买 nike

我想根据某些条件将两个列表合并为一个列表。示例是列表 1 包含

final Org ro1= new Org(1, 1001, "Name 01");
final Org ro2 = new Org (2, 1001, "Name 02");
final Org ro3 = new Org (3, 1002, "Name 03");
final Org ro4 = new Org (4, 1003, "Name 04");
final Org ro5 = new Org (5, 1004, "Name 05");
List<Org> listOrg = new ArrayList<>();
// Add all the object to the listOrg

列表2包含

final Candidate can1 = new Candidate(1001, "Candidate01", "100");
final Candidate can2 = new Candidate(1002, "Candidate02", "150");
final Candidate can3 = new Candidate(1003, "Candidate03", "200");
List<Candidate > listCandidate = new ArrayList<>();
// Add all the Candidate object to the listCandidate

我的最终列表将如下所示

List<Result > listResult  = new ArrayList<>();
// Each individual object of the listResult is followed-
final Result rs1= new Result (1, 1001, "Name 01", "Candidate01", "100");
final Result rs2 = new Result (2, 1001, "Name 02", "Candidate01", "100");
final Result rs3 = new Result (3, 1002, "Name 03", "Candidate02", "150");
final Result rs4 = new Result (4, 1003, "Name 04", "Candidate03", "200");
final Result rs5 = new Result (5, 1004, "Name 05", null, null);

我想使用 Java 8 流功能来实现相同的目的。有人可以帮我吗?

我的类(class)详细信息

public class Candidate {
private int canId;
private String candidateName;
private String score;
//Getter setter with constructors.
}

public class Org{
private int id;
private int canId;
private String name;
//Getter setter with constructors
}

public class Result {
private int id;
private int canId;
private String name;
private String candidateName;
private String score;
//Getter setter with constructors.
}

Org 类有一个 canId,它充当 CandidateClass 的映射点。

最佳答案

你可以这样做,

Map<Integer, Candidate> candidateById = listCandidate.stream()
.collect(Collectors.toMap(Candidate::getId, Function.identity()));
List<Result> resultList = listOrg.stream()
.map(o -> {
Candidate candidate = candidateById.getOrDefault(o.getCandidateId(), new Candidate(-1, null, null));
return new Result(o.getId(), o.getCandidateId(), o.getName(), candidate.getName(), candidate.getNum());
})
.collect(Collectors.toList());

首先根据其 Id 值创建 Candidate 对象的 Map。然后迭代 Org 对象的 List,从给定 Id 值的映射中获取关联的 Candidate 实例,并将它们合并在一起形成一个结果。最后将所有Results收集到List中。

更新

根据下面的评论,这可以进一步改进,

List<Result> resultList = listOrg.stream()
.map(o -> new Result(o, candidateById.get(o.getCandidateId())))
.collect(Collectors.toList());

Result 构造函数现在看起来像这样,

public Result(Org org, Candidate candidate) {
super();
this.orgId = org.getId();
this.candidateId = org.getCandidateId();
this.orgName = org.getName();
this.candidateName = Optional.ofNullable(candidate).map(c -> c.getName()).orElse(null);
this.candidateNum = Optional.ofNullable(candidate).map(c -> c.getNum()).orElse(null);
}

关于java - 根据结果​​数据组合两个列表以在 java 8 中创建一个新列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51130840/

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