草庐IT

terminalOp

全部标签

java - Stream.forEach() 是否总是并行工作?

在AggregatingwithStreams,BrianGoetz比较了使用Stream.collect()填充集合和使用Stream.forEach()执行相同操作,以及以下两个片段:SetuniqueStrings=strings.stream().collect(HashSet::new,HashSet::add,HashSet::addAll);还有,Setset=newHashSet();strings.stream().forEach(s->set.add(s));然后他解释道:Thekeydifferenceisthat,withtheforEach()version,