小编F R*_*F R的帖子

Using Java lambda to build map from two maps

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 …

java lambda java-8 java-stream collectors

7
推荐指数
1
解决办法
145
查看次数

标签 统计

collectors ×1

java ×1

java-8 ×1

java-stream ×1

lambda ×1