Just add a Customer method that returns a Car s stream. Using typical naming conventions, it will look like
public Stream<Car> cars() { return Arrays.stream(cars); }
Then you can use it as
customers.stream().flatMap(Customer::cars)
Generally, properties of a mutable type, such as an array, should be handled with care. The only way to prevent modification through a getter is to make a copy. Thus, providing an alternative method that returns a read-only type, such as Stream , which does not need to be copied, has additional capabilities, in addition to creating a neat flatMap .
source share