我想将我的 Hashmap for 循环转换为 Java8 流。这是我的代码。
int amount = 100;
Map<String, Account> map = accountMap;
for (Map.Entry<String, Account> entry : map.entrySet()) {
String id = entry.getValue().getAccount_id();
updateAccount(amount, id);
}
它会是这样的,但无法确定确切的语法:
accountMap.entrySet().parallelStream()
.collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue()
//Where can I call external method?
));
最佳答案
因为您只使用这些值,所以获取这些值并从中创建一个流。
map.values().parallelStream()
.forEach(value ->
updateAccount(amount, value.getAccount_id()));
关于Java 流式 Hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37526407/