Best way to compile Java-8 thread in Guava ImmutableList

I want to get the stream to an immutable list. What is the difference between the following approaches and which one is better in terms of performance?

  • 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) );

Several options for performance or efficiency,

  • A list / collection can have many entries.

  • What if I want the set to be sorted using the intermediate operation ".sorted()" with a custom comparator.

  • therefore, what if I add .parallel() to the stream
+6
source share
2 answers

I would expect 1) to be most effective: going through additional collectors seems less readable and is unlikely to defeat normal toList() , and copying from an iterator discards size information.

(But Guava is working on adding support for Java 8 for such things that you can just wait.)

+2
source

For system performance, you need to measure it.

For programming performance, use the clearest way. Out of appearance only, I like the second one, and I don’t see obvious inefficiency there.

+3
source

Source: https://habr.com/ru/post/978044/


All Articles