我想将一个流放入一个不可变的列表中。以下方法之间有什么区别,从性能角度来看哪种方法更好?
collect( Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf));
ImmutableList.copyOf( stream.iterator() );
collect( Collector.of( ImmutableList.Builder<Path>::new, ImmutableList.Builder<Path>::add, (l, r) -> l.addAll(r.build()), ImmutableList.Builder<Path>::build) );
性能或效率的更多参数,
列表/集合中可能有很多条目。
如果我想使用中间操作对集合进行排序怎么办
".sorted()"
使用自定义比较器。- 因此,如果我添加
.parallel()
会怎么样?到流
最佳答案
对于系统性能,您需要对其进行测量。
为了编程性能,用最清晰的方式。单从外观上来说,我最喜欢第二种,而且我看不出有明显的效率低下。
关于java - 将 Java-8 Stream 收集到 Guava ImmutableList 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873475/