如何使用 collect 根据某些条件将列表转换为对象?
How to transform list into object based on some conditions using collect?
我想知道在流的 .toCollect 方法中是否有办法根据某些条件将列表转换为对象。这是我的代码。
请求 class:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ERequest {
private String id;
private String cId;
private int pid;
private String ind;
}
响应class:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ExpectedResponse {
private String data1;
private String data2;
private String data3;
}
方法
public ExpectedResponse getPref(List<ERequest> request) {
ExpectedResponse expectedResponse = ExpectedResponse.builder().build();
Predicate<ERequest> isE = p -> (p.getCId().equals("ab")) && (p.getPid() == 1);
Predicate<ERequest> isS = p -> (p.getCId().equals("cd")) && (p.getPid() == 2);
Predicate<ERequest> isP = p -> (p.getCId().equals("ef")) && (p.getPid() == 4);
List<ERequest> res = request.stream()
.filter(isE.or(isS).or(isP))
.collect(Collectors.toList());
for (ERequest r : res) {
if (r.getCId().equals("ab")) {
expectedResponse.setData1(r.getInd());
}
if (r.getCId().equals("cd")) {
expectedResponse.setData2(r.getInd());
}
if (r.getCId().equals("ef")) {
expectedResponse.setData3(r.getInd());
}
}
return expectedResponse;
}
我在 res 中得到一个 valid/required 列表,但我仍然必须使用 for 循环遍历该列表并根据少数条件和然后填充 ExpectedResponse。如何直接将 request 转换为 ExpectedResponse ?
您可以在第一次迭代时使用 filter to filter the records and reduce 执行所有操作,这只是使用 reduce
方法的一种方法,但我没有验证,因为我没有样本数据
ExpectedResponse expectedResponse = requests.stream().filter(req->(req.getCId().equals("ab")) && (req.getPid() == 1) ||
(req.getCId().equals("cd")) && (req.getPid() == 2) ||
(req.getCId().equals("ef")) && (req.getPid() == 4))
.reduce(ExpectedResponse.builder().build(),(response, request)->{
if (request.getCId().equals("ab")) {
response.setData1(request.getInd());
}
if (request.getCId().equals("cd")) {
response.setData2(request.getInd());
}
if (request.getCId().equals("ef")) {
response.setData3(request.getInd());
}
return response;
},(finalRes,finalReq)->finalRes);
我想知道在流的 .toCollect 方法中是否有办法根据某些条件将列表转换为对象。这是我的代码。 请求 class:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ERequest {
private String id;
private String cId;
private int pid;
private String ind;
}
响应class:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ExpectedResponse {
private String data1;
private String data2;
private String data3;
}
方法
public ExpectedResponse getPref(List<ERequest> request) {
ExpectedResponse expectedResponse = ExpectedResponse.builder().build();
Predicate<ERequest> isE = p -> (p.getCId().equals("ab")) && (p.getPid() == 1);
Predicate<ERequest> isS = p -> (p.getCId().equals("cd")) && (p.getPid() == 2);
Predicate<ERequest> isP = p -> (p.getCId().equals("ef")) && (p.getPid() == 4);
List<ERequest> res = request.stream()
.filter(isE.or(isS).or(isP))
.collect(Collectors.toList());
for (ERequest r : res) {
if (r.getCId().equals("ab")) {
expectedResponse.setData1(r.getInd());
}
if (r.getCId().equals("cd")) {
expectedResponse.setData2(r.getInd());
}
if (r.getCId().equals("ef")) {
expectedResponse.setData3(r.getInd());
}
}
return expectedResponse;
}
我在 res 中得到一个 valid/required 列表,但我仍然必须使用 for 循环遍历该列表并根据少数条件和然后填充 ExpectedResponse。如何直接将 request 转换为 ExpectedResponse ?
您可以在第一次迭代时使用 filter to filter the records and reduce 执行所有操作,这只是使用 reduce
方法的一种方法,但我没有验证,因为我没有样本数据
ExpectedResponse expectedResponse = requests.stream().filter(req->(req.getCId().equals("ab")) && (req.getPid() == 1) ||
(req.getCId().equals("cd")) && (req.getPid() == 2) ||
(req.getCId().equals("ef")) && (req.getPid() == 4))
.reduce(ExpectedResponse.builder().build(),(response, request)->{
if (request.getCId().equals("ab")) {
response.setData1(request.getInd());
}
if (request.getCId().equals("cd")) {
response.setData2(request.getInd());
}
if (request.getCId().equals("ef")) {
response.setData3(request.getInd());
}
return response;
},(finalRes,finalReq)->finalRes);