在Java 8中,他们添加了并行流(parallel stream),可以利用多核处理器。你可以像这样使用它:
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));
有人知道如何做类似这样的事情吗?谢谢。
.getValue()
会降低性能吗? - John S.Map.Entry.getValue
和Map.get
搞混了。在这里调用getValue
并不等同于查找Map
。虽然它依赖于具体的map实现,但一个entry通常代表着某种已解析的映射,具有廉价的getKey
和getValue
方法,例如在java.util.HashMap
中,我们是从entry实例中读取字段,而不是进行哈希查找。 - Holger