gpt4 book ai didi

Java 8 | HashMap 的并行流

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:40:26 26 4
gpt4 key购买 nike

在 Java 8 中,我知道他们添加了利用多核处理器的并行流,而且我知道您可以将它与这样的东西一起使用:

List<String> list = new ArrayList<String>();
list.parallelStream().forEach(str -> System.out.println(str));

但是我如何使用 HashMap 实现这样的事情呢?

Map<String, Integer> map = new HashMap<String, Integer>();
// won't work, because the Map class doesn't have the .parallelStream()
map.parallelStream().forEach((str, num) -> System.out.println(str + ":" + num));

有人知道怎么做吗?谢谢

最佳答案

您不能直接流式传输 Map,但您可以流式传输其条目集,给定 the entrySet() method .从条目对象中提取键和值。

map.entrySet()
.parallelStream()
.forEach(entry -> System.out.println(entry.getKey() + ":" + entry.getValue()));

关于Java 8 | HashMap 的并行流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36990108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com