有人可以帮助我使用流将以下代码转换为 java 8 标准吗?
迭代 CustomerRelationship 列表和客户列表,比较 customerRelationShip firstName 和 customer firstName。如果匹配,则使用 customerRelationShip 和客户对象构造 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 与 customer firstName 匹配时,上面的代码才会创建 businessCustomers。期望是:即使没有匹配项,我也想通过使用 customerRelationship 对象来创建 businessCustomers(请检查上面执行此操作的 foreach 代码)。
我相信以下两种方法中的任何一种都有效:
第一个使用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());
你可以或者不能创建两个中间matches
和nonMatches
Stream对象,只是把在这些发言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());
另外,我还没有测试这两种方法,所以一定要通过你自己的一组测试来运行它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句