3

I have a problem with conversion List Object to Map String, List Object. I'm looking for Map with a keys name of all components in cars, and a value is represented by cars with this component

public class Car {
    private String model;
    private List<String> components;
    // getters and setters
}

I write a solution but looking for a better stream solution.

public Map<String, List<Car>> componentsInCar() {
    HashSet<String> components = new HashSet<>();
    cars.stream().forEach(x -> x.getComponents().stream().forEachOrdered(components::add));
    Map<String, List<Car>> mapCarsComponents  = new HashMap<>();
    for (String keys : components) {
        mapCarsComponents.put(keys,
                cars.stream().filter(c -> c.getComponents().contains(keys)).collect(Collectors.toList()));
    }
    return mapCarsComponents;
}

2 Answers 2

2

You could do it with streams too, but I find this a bit more readable:

public static Map<String, List<Car>> componentsInCar(List<Car> cars) {
    Map<String, List<Car>> result = new HashMap<>();
    cars.forEach(car -> {
        car.getComponents().forEach(comp -> {
            result.computeIfAbsent(comp, ignoreMe -> new ArrayList<>()).add(car);
        });
    });

    return result;
}

Or using stream:

public static Map<String, List<Car>> componentsInCar(List<Car> cars) {
    return cars.stream()
               .flatMap(car -> car.getComponents().stream().distinct().map(comp -> new SimpleEntry<>(comp, car)))
               .collect(Collectors.groupingBy(
                   Entry::getKey,
                   Collectors.mapping(Entry::getValue, Collectors.toList())
               ));
}
0

I know this is a Java question, and there is already a Java answer. However, I would like to add that Kotlin, which is a JVM language and perfectly interoperable with Java, you can do things like this very easily and cleanly:

val carsByComponent = cars
    .flatMap { it.components }
    .distinct()
    .map { component -> component to cars.filter { car -> component in car.components } }
    .toMap()

or even more concise, allthough less readable:

val carsByComponent = cars
    .flatMap { car -> car.components.map { it to car } }
    .groupBy { it.first }
    .mapValues {it.value.map { it.second }}

Not the answer you're looking for? Browse other questions tagged or ask your own question.