Q:

Java map with lists of objects to list

Here is my code:
public class CarShop {
    private final HashMap<Brand, List<CarPrototype>> catalog;

    public List<CarPrototype> allPrototypes() {
        List<CarPrototype> prototypes = catalog.values().stream().collect(Collectors.toList())
        return prototypes ;
    } 

What I want to do in this method is I want to get a list of all different car prototypes given in car shop catalog. I should not use for loop, so stream comes into play. My solution is adds only lists of prototypes, and I struggle finding how to change it so it add specific prototypes themselves.

A:

If you have a Stream<List<Foo>> and want to convert it to List<Foo> you can use flatMap:
Stream<List<Foo>> stream = ...;
Stream<Foo> flatStream = stream.flatMap(List::stream);
List<Foo> list = flatStream.collect(Collectors.toList());

For your specific example:
public List<CarPrototype> allPrototypes() {
    List<CarPrototype> prototypes = catalog.values().stream()
        .flatMap(List::stream)
        .collect(Collectors.toList());
    return prototypes;
} 

