gpt4 book ai didi

java - 在两个列表上使用 java 流进行左连接

转载 作者:行者123 更新时间:2023-11-30 05:29:30 25 4
gpt4 key购买 nike

the rest descritpion of tthe main class here is the image for the class description我有一个包含 cid、cname 和 aid 的实体客户,以及另一个包含 help、城市和州的实体地址。

我已经在两个不同的列表 - list 和 list 中获取了两个实体的数据。我想要一个结果列表(客户和地址上的左连接),其中包含使用 java 流 api 或 java 8 的任何其他功能的两个列表中的数据。以及结果列表的类型应该是什么。?

可以这样做吗?

请帮忙。

提前致谢。

<小时/>

公开课客户{

private Integer cid;
private String name;
private Integer aid;
// getters and setters
// tostring()
// constructors with and without params

公开课地址{

private Integer aid;
private String city;
private String state;
private Integer pincode;
//getters and setters
//tostring()
//constructors with and without params

公共(public)类 Cust_Add_DTO {

private Integer cid;
private String name;
private Integer aid;
private String city;
private String state;
private Integer pincode;
// getters and setters
// tostring()
// constructors with and without params

公共(public)类 DemoMain {

public static void main(String[] args) {

List<Customers> customers = new ArrayList<Customers>();
List<Address> addresses = new ArrayList<Address>();
customers.add(new Customers(1, "abc1", 123));
customers.add(new Customers(2, "abc2", 124));
customers.add(new Customers(3, "abc3", 125));
customers.add(new Customers(4, "abc4", 126));
customers.add(new Customers(5, "abc5", 127));

addresses.add(new Address(123, "bangalore", "karnataka", 101010));
addresses.add(new Address(125, "chennai", "tamil nadu", 202020));
addresses.add(new Address(127, "hyderabad", "telanagana", 303030));

List<Cust_Add_DTO> mergerdleftjoin = customers.stream()
.flatMap(x -> addresses.stream().filter(y -> x.getAid().equals(y.getAid())))
.map(y -> new Cust_Add_DTO(x.getCid(), y.getAid(), y.getCity(), y.getPincode(), y.getState(),
x.getName()))
.collect(Collectors.toList());

最佳答案

我可以看到您有来自数据库的两个实体的列表:

// @Entity
class Customers {

private int cid;
private String name;
private int aid;
}

// @Entity
class Address {

private int aid;
private String city;
private String state;
}

最好将DAO层实体从DTO中分离出来;所以你应该创建所需的 DTO:

class CustomerDTO {

private int cid;
private String name;
private AddressDTO address;
}

class AddressDTO {

private int aid;
private String city;
private String state;
}

现在您已准备好使用 Streams 编写 leftJoin 方法:

public static List<CustomerDTO> leftJoin(List<Customers> customers, List<Address> addresses) {
Map<Integer, Address> aidAddress = addresses.stream().collect(Collectors.toMap(Address::getAid, Function.identity()));

return customers.stream()
.map(customer -> {
CustomerDTO customerDto = new CustomerDTO();
// set all fields from customer -> customerDto

Address address = aidAddress.get(customer.getAid());

if (address != null) {
AddressDTO addressDto = new AddressDTO();
// set all fields from address -> addressDto
customerDto.setAddress(addressDto);
}

return customerDto;
})
.collect(Collectors.toList());
}

关于java - 在两个列表上使用 java 流进行左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57865058/

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