I have some code that I need help with ... I'm trying to build a map using two maps as a source and at the same time using java lambdas
Map<String, List<String>> motorsMapping = new HashMap<>();
motorsMapping.put("CAR", Collections.singletonList("AUDI"));
motorsMapping.put("CAR_AND_BIKE", Arrays.asList("AUDI", "BMW"));
motorsMapping.put("BIKE", Collections.singletonList("BMW"));
Map<String, List<String>> models = new HashMap<>();
models.put("AUDI", Arrays.asList("A1", "Q2"));
models.put("BMW", Arrays.asList("X1", "X5"));
motorsMapping.keySet().forEach(key -> {
Map<String, List<String>> result = new HashMap<>();
result.put(key, new ArrayList<>());
motorsMapping.get(key).forEach(value -> models.getOrDefault(value, Collections.emptyList()).forEach(property -> result.get(key).add(property)));
});
Run Code Online (Sandbox Code Playgroud)
I can do that with the …