Say we have a Customer
class:
public class Customer {
private Car[] cars;
// getter, setter, constructor
}
and collection of customers which we need to map on cars.
Currently I'm doing it somehow like this:
Collection<Customer> customers = ...
customers.stream().flatMap(
customer -> Arrays.stream(customer.getCars())
)...
It works well, but the code doesn't look elegant. I'd really like to replace it with code that uses method references which usually looks more readable and more compact. But using a field of array type makes it hard.
Question: is there any way of enhancing the flatMap
call so it will be more readable/compact/clear?