The car has many manufactures, and I want to collect all the manufacturers in the kit. For instance:
class Car { String name, List<String> manufactures. } object sedan -> { ford, gm, tesla } object sports -> { ferrari, tesla, bmw } object suv -> { ford, bmw, toyota }
Now I need to create an output to contain all manufacturers (without redundancy)
Now:
carList.stream().map(c -> c.getManufacturers()).collect(Collectors.toSet());
This gives me a set of lists. But I need to get rid of nesting and just create one set (not nested).
[EDIT] What if some objects are null for manufacturers and we want to prevent NPE?
source share