LEFT JOIN在两个列表使用Java流

Nilesh制作肖汉:

里边反主类的其他descritpion 这里是类描述的图像我有一个实体的客户与CID,CNAME和援助,并与援助,城市和国家其他实体的地址。

我已经采取了两种不同的列表中两个实体数据-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());
oleg.cherednik:

我可以看到你从DB两个实体的名单:

// @Entity
class Customers {

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

// @Entity
class Address {

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

这是更好地从DTO分裂DAO层实体; 所以你应该创建所需的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());
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章