Compare the List of different Objects and build the new list of Object in java8 using Stream


Compare the List of different Objects and build the new list of Object in java8 using Stream



Could someone help me converting the below code to java 8 standards using streams.



Iterate CustomerRelationship list and customer list,compare customerRelationShip firstName with customer firstName. if it matches then construct the BusinessCustomer object using customerRelationShip and customer object and add it to businessCustomerList. if no matches then construct BusinessCustomer using customerRelationShip and add it to 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);
}
}



I have develop something like this using streams.


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



Above code creates the businessCustomers only when the customerRelationship firstName matches the customer firstName. Expectation is : I want to create the businessCustomers even when there is no matches by using the customerRelationship Object (please check the above foreach code which does the thing).





Show expected output and actual output, and explain clearly what is not working.
– Jim Garrison
Jun 30 at 3:10




2 Answers
2



I believe that either of the following two approaches would work:



This first one uses Stream.concat which enables you to put two Streams together.


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



You could alternatively not create the two intermediate matches and nonMatches Stream objects and just put those statements in the Stream.concat.


matches


nonMatches


Stream.concat



The other method I think would work is the following:


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



Also, I have yet to test these two approaches, so be sure to run them through your own set of tests.





Thanks concat stuff working!! cheers :)
– star
2 days ago


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





@Thanks Rafael, your solution looks pretty close. steamAges method return just age.I am expecting to return List<Customer>.how do i handle null if the StreamAges return null?
– star
Jun 30 at 9:25





Stream.of(null) return NullPointerException also
– star
Jun 30 at 9:27





@star, changed streamAges method to streamBusinessCustomers. It makes no assumption on getAge returning type, uses the BusinessCustomer constructor logic in your original code and avoids an "empty" (or null) Customer (the List<Customer> implies) to create a BusinessCustomer with no age.
– Rafael
Jun 30 at 14:58





@thanks for your effort.I have to build the businessCustomer object using customerRelationship object when sameFirstNameCustomers.size() == 0. if sameFirstNameCustomers.size() > 0 , I can build the business customer using both Customer and customerRelationship object.
– star
2 days ago





@star, that's what it does.
– Rafael
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV