简体   繁体   中英

Java 8 List to Map conversion

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;
}

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())
               ));
}

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 }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM