我有这样的代码:
Map<Integer, Settings> operatorsSettings = new HashMap<>();
operators.forEach((operator, codeTypes) -> operatorsSettings.put(operator, mapper.convertValue(codeTypes.get(SETTINGS), Settings.class)));
return operatorsSettings;
我写了它,但我想知道。是否可以在不创建新 map 的情况下编写它。像这样的东西(不正确的代码):
return operators.entrySet().stream()
.collect(entry -> Collectors.toMap(entry.getKey() , mapper.convertValue(entry.getValue().get(SETTINGS), Settings.class)));
最佳答案
有可能,你只是犯了一个小语法错误......
return operators.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey() , mapper.convertValue(entry.getValue().get(SETTINGS), Settings.class)));
关于java - 通过流从其他 map 收集 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728260/