Java 按流从其他地图收集地图
我有这样的代码:Java 按流从其他地图收集地图,java,dictionary,java-8,functional-programming,java-stream,Java,Dictionary,Java 8,Functional Programming,Java Stream,我有这样的代码: Map<Integer, Settings> operatorsSettings = new HashMap<>(); operators.forEach((operator, codeTypes) -> operatorsSettings.put(operator, mapper.convertValue(codeTypes.get(SETTINGS), Settings.class))); return operatorsSettings;
Map<Integer, Settings> operatorsSettings = new HashMap<>();
operators.forEach((operator, codeTypes) -> operatorsSettings.put(operator, mapper.convertValue(codeTypes.get(SETTINGS), Settings.class)));
return operatorsSettings;
是的,您可以在纯java中执行此操作:
return operators.entrySet().stream()
.collect(Collectors.toMap(entry -> entry.getKey() , entry -> mapper.convertValue(entry.getValue().get(SETTINGS), Settings.class)));
或者,您可以使用streamex库并按如下方式编写:
EntryStream.of(operatorsSettings).mapValues(codeTypes -> mapper.convertValue(codeTypes.get(SETTINGS), Settings.class))...
是的,您可以在纯java中执行此操作:
return operators.entrySet().stream()
.collect(Collectors.toMap(entry -> entry.getKey() , entry -> mapper.convertValue(entry.getValue().get(SETTINGS), Settings.class)));
或者,您可以使用streamex库并按如下方式编写:
EntryStream.of(operatorsSettings).mapValues(codeTypes -> mapper.convertValue(codeTypes.get(SETTINGS), Settings.class))...
有可能,您只是犯了一个小的语法错误
return operators.entrySet().stream().collect(Collectors.toMap(entry -> 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)));
你试过用吗?你试过用吗?