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

时间:2019-09-10 06:22:15

标签: java java-8

the rest descritpion of tthe main class here is the image for the class description我有一个具有cid,cname和aid的客户实体,以及一个有aid,城市和州的实体Address。

我在两个不同的列表-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

公共类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

公共类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());

1 个答案:

答案 0 :(得分:1)

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

// @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;
}

现在,您可以使用leftJoin来编写Streams方法了:

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());
}