Class MoreCollectors

java.lang.Object
com.atlassian.bitbucket.util.MoreCollectors

public class MoreCollectors extends Object
Additional utility methods missing from Collectors. The implementation of these collectors, where possible, attempts to emulate the behavior of the standard Collectors factories.
  • Method Details

    • toImmutableList

      @Nonnull public static <T> Collector<T,?,List<T>> toImmutableList()
      Returns a Collector that accumulates elements into an immutable list.

      The Stream being collected may contain null elements and, when it does, the collected list will also contain null elements.

      Type Parameters:
      T - the type of the input elements
      Returns:
      a Collector which collects elements into an immutable list
      See Also:
    • toImmutableMap

      @Nonnull public static <T, K, U> Collector<T,?,Map<K,U>> toImmutableMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
      Returns a Collector that accumulates elements into an immutable map whose keys and values are the result of applying the provided mapping functions to the input elements.

      The Stream being collected may contain null elements, if the mapping functions handle nulls. However, like Collectors.toMap(Function, Function), this collector uses a HashMap to collect keys and values. Since its implementation is uses Map.merge(K, V, java.util.function.BiFunction<? super V, ? super V, ? extends V>), the valueMapper may not produce null values, even for null inputs, or a NullPointerException will be thrown. The keyMapper, on the other hand, may return null keys, but may not return duplicate keys or an IllegalStateException will be thrown.

      Type Parameters:
      T - the type of the input elements
      K - the output type of the key mapping function
      U - the output type of the value mapping function
      Parameters:
      keyMapper - a mapping function to produce keys, which may return null
      valueMapper - a mapping function to produce values, which may not return null
      Returns:
      a Collector which collects elements into an immutable map
      Since:
      4.8
      See Also:
    • toImmutableSet

      @Nonnull public static <T> Collector<T,?,Set<T>> toImmutableSet()
      Returns a Collector that accumulates elements into an immutable set.

      The Stream being collected may contain null elements and, when it does, the collected set will also contain a null element. Unlike Collectors.toSet(), this collector attempts to retain the traversal order of the stream's elements.

      Type Parameters:
      T - the type of the input elements
      Returns:
      a Collector which collects elements into an immutable set
      See Also: