在两个列表上使用Java流进行左连接 [英] Left join using java stream on two lists

查看:1547
本文介绍了在两个列表上使用Java流进行左连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要类别的其余说明

the rest descritpion of tthe main classhere is the image for the class descriptionI have an entity Customers with cid,cname and aid and another entity Address with aid, city and state.

我在两个不同的列表-list和list中获取了两个实体的数据.我想要一个结果列表(在客户和地址上都保持左联接),该结果列表包含使用java流api或java 8的任何其他功能来自两个列表的数据,并且结果列表的类型应该是什么??

I have taken data of both the entities in two different lists -list and list . i want a result list (left join on both customer and address)that contains data from both the lists using java stream api or any other feature of java 8. and further what should be the type of result list.?

有可能这样做吗??

请帮助.

谢谢.

公共类客户{

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 {

public class 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 class 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());

推荐答案

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

I can see that you have a list of two entities from 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:

It's better to split DAO layer entities from DTO; so you should create required 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方法了:

Right now you are ready to write a leftJoin method using 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());
}

这篇关于在两个列表上使用Java流进行左连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆