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.
".sorted()"
.parallel()
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.
toList()
(But Guava is working on adding support for Java 8 for such things that you can just wait.)
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.
Source: https://habr.com/ru/post/978044/More articles:How can I access a low-level client from an instance of a Boto 3 resource? - pythonprint max value in dict with key - pythonASP.NET Web API OData: Navigation Links Using Compound Keys - odataHow can I get a contour plot superimposed on a base map - pythonOffice 365 SharePoint v1.0 Authorization Error - sharepointKnowledge of Bluetooth Beacons / iBeacons - javascriptSetting Unicode Header from Resource DLL String Table - dllJSON Serializer object with internal properties - jsonIs access to private fields and properties a reflection of a security issue? - reflectionActionView :: Template :: Error (PG :: UndefinedFunction: ERROR: statement does not exist: integer ~~ unknown - ruby ββ| fooobar.comAll Articles