kdaplan.blogg.se

Empty mutable list kotlin
Empty mutable list kotlin







empty mutable list kotlin

That is, elements of a set cannot be duplicate. As the name implies, we want the collector to work pretty much the same as the standard toList() collector, except that when the result list is empty, the collector will return null instead of an empty list: Collector, ArrayList> emptyListToNullCollector = Collector.A set is a collection of unique data. Next, let's create a custom collector called emptyListToNullCollector.

empty mutable list kotlin

However, we can make use of this finisher function to ask the collector to return a nullable container. This is because many collectors can simply use the mutable container as the final result. We should note that the last argument, the finisher function, is optional.

  • An optional finisher function – Take the mutable result container and perform the required final transformations on it before returning the final result of the collect operation.
  • A combiner function – Merge the intermediate results into a single final result container when the stream is processed in parallel.
  • An accumulator function – Modify the mutable container to incorporate the current element.
  • A Supplier function – Return a mutable result container for the collect operation.
  • The Collector.of() method takes four arguments: Stream API has provided the static Collector.of() method that allows us to create a custom collector. However, if we can create our own collector that returns nullable results, llect() may return null, too. Therefore, the llect() method won't return null, either.

    empty mutable list kotlin

    The standard collectors don't return null. So, all standard collectors won't return null. Instead, they produce an empty list or map as their result. Then, as we can see, the toList(), toMap(), and groupingBy() collectors don't return null. In the tests above, the filter(s -> s != null & s.length() = 1) method will return an empty stream as no element matches the condition. collect(toMap(s -> s.charAt(0), Function.identity())) Next, let's take three commonly used collectors to verify this: List result = LANGUAGES.stream() In that case, the collect() method will return an empty result container, such as an empty List, an empty Map, or an empty array, depending on the collector used in the collect() method. Suppose the stream to be collected is empty. When we use the standard collectors, the collect() method of the Java Stream API will not return null even if the stream is empty.









    Empty mutable list kotlin