比较不同对象的列表,并使用Stream在java8中构建新的对象列表

时间:2018-06-30 03:09:00

标签: java java-8

有人可以帮助我使用流将以下代码转换为Java 8标准吗?

迭代CustomerRelationship列表和客户列表,将customerRelationShip firstName与客户firstName进行比较。如果匹配,则使用customerRelationShip和customer对象构造BusinessCustomer对象,并将其添加到businessCustomerList。如果没有匹配项,则使用customerRelationShip构造BusinessCustomer并将其添加到businessCustomerList。

List<BusinessCustomer> businessCustomers = new ArrayList<BusinessCustomer>();

List<CustomerRelationship> customerRelationshipList = new ArrayList<CustomerRelationship>();

       List<Customer> customerList = new ArrayList<Customer>();

        for (CustomerRelationship customerRelationship: customerRelationshipList) {
            int temp = 0;
            for (Customer customer:customerList){
                if(customer.getFirstName().equalsIgnoreCase(customerRelationship.getFirstName()))
                {
                    temp++;
                    BusinessCustomer b = new BusinessCustomer();
                    b.setAge(customer.getAge());
                    b.setFirstName(customerRelationship.getFirstName());
                    b.setLastName(customerRelationship.getLastName());
                    businessCustomers.add(b);
                }
            }
            if(temp == 0) {
                BusinessCustomer b = new BusinessCustomer();                
                b.setFirstName(customerRelationship.getFirstName());
                b.setLastName(customerRelationship.getLastName());
                businessCustomers.add(b);
            }
        }

我已经使用流开发了类似的东西。

List<CustomerRelationship> customerRelationshipList = Fetch from the Table (CustomerRelationship)
       List<Customer> customerList = Fetch from the Table (Customer)
       List<BusinessCustomer> businessCustomers = customerRelationshipList.stream()
                .flatMap(c -> customerList.stream()
                .filter((d -> (c.getFirstName()
                                .equals(d.getFirstName()))
                        ))
                        .map(d -> new BusinessCustomer(c.getFirstName(),c.getLastName(),d.getAge()))
                .collect(Collectors.toList());

仅当customerRelationship firstName与客户firstName匹配时,以上代码才会创建businessCustomers。期望是:即使在没有匹配项的情况下,我也要使用customerRelationship对象来创建businessCustomers(请检查上面的foreach代码是做什么的)。

2 个答案:

答案 0 :(得分:1)

我相信以下两种方法都可以使用:

第一个使用Stream.concat,使您可以将两个流放在一起。

    Stream<BusinessCustomer> matches = customerRelationships.stream()
            .flatMap(relationship -> customers.stream()
                    .filter(customer -> customer.getFirstName().equalsIgnoreCase(relationship.getFirstName()))
                    .map(customer -> new BusinessCustomer(relationship.getFirstName(), relationship.getLastName(), customer.getAge())));

    Stream<BusinessCustomer> nonMatches = customerRelationships.stream()
            .filter(relationship -> customers.stream().noneMatch(customer -> customer.getFirstName().equalsIgnoreCase(relationship.getFirstName())))
            .map(relationship -> new BusinessCustomer(relationship.getFirstName(), relationship.getLastName()));

    List<BusinessCustomer> result = Stream.concat(matches, nonMatches)
            .collect(Collectors.toList());

或者您不能创建两个中间matchesnonMatches流对象,而只是将这些语句放在Stream.concat中。

我认为其他可行的方法如下:

    customerRelationships.stream()
            .flatMap(relationship -> {
                boolean noneMatch = customers.stream().noneMatch(customer -> customer.getFirstName().equalsIgnoreCase(relationship.getFirstName()));
                if (noneMatch) {
                    return Stream.of(new BusinessCustomer(relationship.getFirstName(), relationship.getLastName()));
                } else {
                    return customers.stream()
                            .filter(customer -> customer.getFirstName().equalsIgnoreCase(relationship.getFirstName()))
                            .map(customer -> new BusinessCustomer(relationship.getFirstName(), relationship.getLastName(), customer.getAge()));
                }
            })
            .collect(Collectors.toList());

此外,我尚未测试这两种方法,因此请确保通过您自己的一组测试来运行它们。

答案 1 :(得分:0)

public List<BusinessCustomer> listBusinessCustomers(List<CustomerRelationship> customerRelationships, List<Customer> customers) {
    return customerRelationships.stream()
        .flatMap(cr -> streamBusinessCustomers(customers, cr.getFirstName(), cr.getLastName()))
        .collect(Collectors.toList());
}

private Stream<BusinessCustomer> streamBusinessCustomers(List<Customer> customers, String firstName, String lastName) {
    List<Customer> sameFirstNameCustomers = customers.stream()
        .filter(customer -> customer.getFirstName().equalsIgnoreCase(firstName))
        .collect(Collectors.toList());

    if (sameFirstNameCustomers.size() == 0) {
        return Stream.of(new BusinessCustomer(firstName, lastName));
    }

    return sameFirstNameCustomers.stream()
        .map(customer -> new BusinessCustomer(firstName, lastName, customer.getAge()));
}