Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Join Map with Collectors.toMap

I'm trying to collect in a Map the results from the process a list of objects and that it returns a map. I think that I should do it with a Collectors.toMap but I haven't found the way.

This is the code:

public class Car {
    List<VersionCar> versions;
    public List<VersionCar> getVersions() {
        return versions;
      }
    }

public class VersionCar {
  private String wheelsKey;
  private String engineKey;
  public String getWheelsKey() {
     return wheelsKey;
  }
  public String getEngineKey() {
    return engineKey;
  }
}

process method:

private static Map<String,Set<String>> processObjects(VersionCar version) {
    Map<String,Set<String>> mapItems = new HashMap<>();
    mapItems.put("engine", new HashSet<>(Arrays.asList(version.getEngineKey())));
    mapItems.put("wheels", new HashSet<>(Arrays.asList(version.getWheelsKey())));
    return mapItems;
}

My final code is:

Map<String,Set<String>> mapAllItems =
                car.getVersions().stream()
                   .map(versionCar -> processObjects(versionCar))
                   .collect(Collectors.toMap()); // here I don't know like collect the map.

My idea is to process the list of versions and in the end get a Map with two items: wheels and engine but with a set<> with all different items for all versions. Do you have any ideas as can I do that with Collectors.toMap or another option?

like image 806
jose rivera Avatar asked Feb 10 '26 10:02

jose rivera


2 Answers

The operator you want to use in this case is probably "reduce"

car.getVersions().stream()
        .map(versionCar -> processObjects(versionCar))
        .reduce((map1, map2) -> {
                    map2.forEach((key, subset) -> map1.get(key).addAll(subset));
                    return map1;
                })
        .orElse(new HashMap<>());

The lambda used in "reduce" is a BinaryOperator, that merges 2 maps and return the merged map.

The "orElse" is just here to return something in the case your initial collection (versions) is empty. From a type point of view it gets rid of the "Optional"

like image 51
stanislas Pocthier Avatar answered Feb 13 '26 02:02

stanislas Pocthier


You can use Collectors.toMap(keyMapper, valueMapper, mergeFunction). Last argument is used to resolve collisions between values associated with the same key.

For example:

    Map<String, Set<String>> mapAllItems =
            car.getVersions().stream()
                    .map(versionCar -> processObjects(versionCar))
                    .flatMap(m -> m.entrySet().stream())
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                            (firstSet, secondSet) -> {
                                    Set<String> result = new HashSet<>();
                                    result.addAll(firstSet);
                                    result.addAll(secondSet);
                                    return result;
                            }
                    ));
like image 39
Vlad Bochenin Avatar answered Feb 13 '26 00:02

Vlad Bochenin