Left join using java stream on two lists

Nilesh Chauhan :

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.

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.?

is it possible to do so.?

please help.

Thanks in advance.


public class Customers {

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

public class Address {

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

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

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());
oleg.cherednik :

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

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

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329031&siteId=1