Interface BiCollector<K,V,R>

Type Parameters:
K - the key type
V - the value type
R - the result type
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface BiCollector<K,V,R>
Logically, a BiCollector collects "pairs of things", just as a Collector collects "things".

BiCollector is usually passed to BiStream.collect(com.google.mu.util.stream.BiCollector<? super K, ? super V, R>). For example, to collect the input pairs to a ConcurrentMap:


 ConcurrentMap<String, Integer> map = BiStream.of("a", 1).collect(Collectors::toConcurrentMap);
 

In addition to the common implementations provided by BiCollectors, many Collector-returning factory methods can be directly "method referenced" as BiCollector if the method accepts two Function parameters corresponding to "key" and "value" respectively. For example: Collectors::toConcurrentMap, ImmutableSetMultimap::toImmutableSetMultimap, Maps::toImmutableEnumMap and ImmutableBiMap::toImmutableBiMap etc.

  • Method Summary

    Modifier and Type
    Method
    Description
    <E> Collector<E,?,R>
    collectorOf(Function<E,K> toKey, Function<E,V> toValue)
    Returns a Collector such that the keys (as returned by the toKey function) and values (as returned by the toValue function) of the input elements are collected by this BiCollector.
  • Method Details

    • collectorOf

      <E> Collector<E,?,R> collectorOf(Function<E,K> toKey, Function<E,V> toValue)
      Returns a Collector such that the keys (as returned by the toKey function) and values (as returned by the toValue function) of the input elements are collected by this BiCollector.

      Conceptually toImmutableMap(Employee::id, Employee::manager) is equivalent to BiCollectors.toImmutableMap().collectorOf(Employee::id, Employee::manager).

      Type Parameters:
      E - used to abstract away the underlying pair/entry type used by BiStream.
      Parameters:
      toKey - The function to read the key from the input entry. May be applied on the same input entry multiple times. Because input entries could be ephemeral like Map.Entry, applying the function on a previous input entry has undefined result.
      toValue - The function to read the value from the input entry. May be applied on the same input entry multiple times. Because input entries could be ephemeral like Map.Entry, applying the function on a previous input entry has undefined result.
      Since:
      6.6