Class GuavaCollectors
- Since:
- 4.7
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
B> Collector <T, ?, com.google.common.collect.ImmutableMultiset<B>> countingBy
(Function<? super T, ? extends B> bucketer) Returns a collector that counts the number of occurrences for each unique bucket as determined by thebucketer
function.static <K,
T, V> BiCollector <K, T, com.google.common.collect.ImmutableListMultimap<K, V>> flatteningToImmutableListMultimap
(Function<? super T, ? extends Stream<? extends V>> flattener) Returns aBiCollector
that first flattens each value of the input pair withflattener
, and then collects the flattened pairs into anImmutableListMultimap
.static <K,
T, V> BiCollector <K, T, com.google.common.collect.ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap
(Function<? super T, ? extends Stream<? extends V>> flattener) Returns aBiCollector
that first flattens each value of the input pair withflattener
, and then collects the flattened pairs into anImmutableSetMultimap
.static <K,
V> Collector <V, ?, com.google.common.collect.ImmutableMap<K, V>> indexingBy
(Function<? super V, ? extends K> indexingFunction) Returns aCollector
that collects to anImmutableMap
with the input elements uniquely indexed by the return value ofindexingFunction
.static <T> Collector
<T, ?, Both<com.google.common.collect.ImmutableList<T>, com.google.common.collect.ImmutableList<T>>> partitioningBy
(Predicate<? super T> predicate) Returns a collector that partitions the incoming elements into two groups: elements that matchpredicate
, and those that don't.static <K extends Comparable<K>,
V>
BiCollector<com.google.common.collect.Range<K>, V, BiStream<com.google.common.collect.Range<K>, Chain<V>>> Returns a BiCollector that merges values mapped to overlapping ranges into aChain
, which is an immutableList
that may have been deeplyconcatenated
.static <K extends Comparable<K>,
V>
BiCollector<com.google.common.collect.Range<K>, V, BiStream<com.google.common.collect.Range<K>, V>> toDisjointRanges
(BinaryOperator<V> merger) Returns a BiCollector that merges values mapped to overlapping ranges using themerger
function and builds aBiStream
with disjoint ranges and the corresponding merged values.static <K extends Comparable<K>,
V, R>
BiCollector<com.google.common.collect.Range<K>, V, BiStream<com.google.common.collect.Range<K>, R>> toDisjointRanges
(Collector<V, ?, R> valueCollector) Returns a BiCollector that collects values mapped to overlapping ranges usingvalueCollector
, and builds aBiStream
with disjoint ranges and the corresponding collector results.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableBiMap<K, V>> Returns aBiCollector
that collects the key-value pairs into anImmutableBiMap
.static <T,
K, V> Collector <T, ?, com.google.common.collect.ImmutableBiMap<K, V>> toImmutableBiMap
(Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableBiMap
.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableListMultimap<K, V>> Returns aBiCollector
that collects the key-value pairs into anImmutableListMultimap
.static <T,
K, V> Collector <T, ?, com.google.common.collect.ImmutableListMultimap<K, V>> toImmutableListMultimap
(Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableListMultimap
.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableMap<K, V>> Returns aBiCollector
that collects the key-value pairs into anImmutableMap
.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableMap<K, V>> toImmutableMap
(BinaryOperator<V> valueMerger) Returns aBiCollector
that collects the key-value pairs into anImmutableMap
usingvalueMerger
to merge values of duplicate keys.static <T,
K, V> Collector <T, ?, com.google.common.collect.ImmutableMap<K, V>> toImmutableMap
(Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableMap
.static <K,
V1, V> BiCollector <K, V1, com.google.common.collect.ImmutableMap<K, V>> toImmutableMap
(Collector<V1, ?, V> valueCollector) Returns aBiCollector
that collects the key-value pairs into anImmutableMap
usingvalueCollector
to collect values of identical keys into a final value of typeV
.static <T,
K, V> Collector <T, ?, com.google.common.collect.ImmutableMap<K, V>> toImmutableMapIgnoringDuplicateEntries
(Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableListMultimap
.static <T,
K, V> Collector <T, ?, com.google.common.collect.ImmutableMap<K, V>> toImmutableMapIgnoringDuplicateEntries
(Function<? super T, ? extends K> toKey, Function<? super T, ? extends V> toValue) Returns aCollector
that accumulates elements into anImmutableMap
whose keys and values are the result of applying the provided mapping functions to the input elements.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableMultiset<K>> toImmutableMultiset
(ToIntFunction<? super V> countFunction) Returns aBiCollector
that collects the key-value pairs into anImmutableMultiset
whose elements are the keys, with counts equal to the result of applyingcountFunction
to the values.static <K extends Comparable<K>,
V>
BiCollector<com.google.common.collect.Range<K>, V, com.google.common.collect.ImmutableRangeMap<K, V>> Returns a BiCollector that takes input (disjoint) ranges and the corresponding values and builds anImmutableRangeMap
.static <K extends Comparable<K>,
V>
BiCollector<com.google.common.collect.Range<K>, V, com.google.common.collect.ImmutableRangeMap<K, V>> toImmutableRangeMap
(BinaryOperator<V> merger) Returns a BiCollector that merges values mapped to overlapping ranges using themerger
function and builds anImmutableRangeMap
with disjoint ranges and the corresponding merged values.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableSetMultimap<K, V>> Returns aBiCollector
that collects the key-value pairs into anImmutableSetMultimap
.static <T,
K, V> Collector <T, ?, com.google.common.collect.ImmutableSetMultimap<K, V>> toImmutableSetMultimap
(Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableSetMultimap
.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableSortedMap<K, V>> toImmutableSortedMap
(Comparator<? super K> comparator) Returns aBiCollector
that collects the key-value pairs into anImmutableSortedMap
according tocomparator
.static <K,
V> BiCollector <K, V, com.google.common.collect.ImmutableSortedMap<K, V>> toImmutableSortedMap
(Comparator<? super K> comparator, BinaryOperator<V> valueMerger) Returns aBiCollector
that collects the key-value pairs into anImmutableSortedMap
according tocomparator
, usingvalueMerger
to merge values of duplicate keys.static <R,
C, V> BiCollector <R, BiStream<? extends C, ? extends V>, com.google.common.collect.ImmutableTable<R, C, V>> Returns aBiCollector
that collects the key-value pairs into ancom.google.common.collect.ImmutableTable<R,
, where each input key (of typeC, V> R
) is mapped to a row in the table, and each input value is aBiStream<C, V>
whose keys (of typeC
) are mapped to columns in the table, and whose values (of typeV
) are mapped to the cell values.static <T,
R, C, V>
Collector<T, ?, com.google.common.collect.ImmutableTable<R, C, V>> toImmutableTable
(Function<? super T, ? extends R> rowFunction, Function<? super T, ? extends C> columnFunction, Collector<T, ?, V> cellCollector) Returns a collector that maps each value into a row-key and column-key for a table, and then collects all values mapped to the same cell usingcellCollector
.static <F,
T> Collector <F, ?, com.google.common.collect.ImmutableList<T>> Returns a collector that collects the results of applying themapper
function on the input elements into anImmutableList
.static <K,
V, M extends com.google.common.collect.Multimap<K, V>>
BiCollector<K, V, M> toMultimap
(Supplier<M> multimapSupplier) Returns aBiCollector
that collects the key-value pairs into aMultimap
created withmultimapSupplier
.static <K extends Comparable<K>,
V, M extends com.google.common.collect.RangeMap<K, V>>
BiCollector<com.google.common.collect.Range<K>, V, M> toRangeMap
(Supplier<? extends M> factory, BinaryOperator<V> merger) Returns a BiCollector that merges values mapped to overlapping ranges using themerger
function and builds a mutableRangeMap
of typeM
that's created byfactory
, and populated with disjoint ranges and the corresponding merged values.static <F,
T> Collector <F, ?, com.google.common.collect.ImmutableSet<T>> Returns a collector that collects the results of applying themapper
function on the input elements into anImmutableSet
.
-
Method Details
-
toImmutableMap
Returns aBiCollector
that collects the key-value pairs into anImmutableMap
.Normally calling
biStream.toMap()
is more convenient, but for example when you've got aBiStream<K, LinkedList<V>>
, and need to collect it intoImmutableMap<K, List<V>>
, you'll need to callcollect(toImmutableMap())
instead oftoMap()
. -
toImmutableMap
public static <K,V> BiCollector<K,V, toImmutableMapcom.google.common.collect.ImmutableMap<K, V>> (BinaryOperator<V> valueMerger) Returns aBiCollector
that collects the key-value pairs into anImmutableMap
usingvalueMerger
to merge values of duplicate keys. -
countingBy
public static <T,B> Collector<T,?, countingBycom.google.common.collect.ImmutableMultiset<B>> (Function<? super T, ? extends B> bucketer) Returns a collector that counts the number of occurrences for each unique bucket as determined by thebucketer
function. The result counts are stored in anImmutableMultiset
, hence implying thatbucketer
cannot return null.stream.collect(countingBy(User::id))
is equivalent tostream.map(User::id).collect(toImmutableMultiset())
, but reads more intuitive when you are trying to count occurrences (as opposed to building Multiset as the end goal).Alternatively, one can use
groupingBy(bucketer, Collectors.counting())
to collect the equivalent counts in anImmutableMap<B, Long>
. Which of the two types to use depends on whether you need to handle very large counts (potentially exceedingInteger.MAX_VALUE
), or whether theMultiset
API is more useful (particularly,Multiset.count(java.lang.Object)
). The memory footprint ofImmutableMultiset<B>
is also more compact thanImmutableMap<B, Long>
.- Since:
- 5.6
-
indexingBy
public static <K,V> Collector<V,?, indexingBycom.google.common.collect.ImmutableMap<K, V>> (Function<? super V, ? extends K> indexingFunction) Returns aCollector
that collects to anImmutableMap
with the input elements uniquely indexed by the return value ofindexingFunction
. -
toImmutableMap
public static <K,V1, BiCollector<K,V> V1, toImmutableMapcom.google.common.collect.ImmutableMap<K, V>> (Collector<V1, ?, V> valueCollector) Returns aBiCollector
that collects the key-value pairs into anImmutableMap
usingvalueCollector
to collect values of identical keys into a final value of typeV
.For example, the following calculates total population per state from city demographic data:
ImmutableMap<StateId, Integer> statePopulations = BiStream.from(cities, City::getState, c -> c) .collect(toImmutableMap(summingInt(City::getPopulation)));
Entries are collected in encounter order.
-
toImmutableSortedMap
public static <K,V> BiCollector<K,V, toImmutableSortedMapcom.google.common.collect.ImmutableSortedMap<K, V>> (Comparator<? super K> comparator, BinaryOperator<V> valueMerger) Returns aBiCollector
that collects the key-value pairs into anImmutableSortedMap
according tocomparator
, usingvalueMerger
to merge values of duplicate keys. -
toImmutableSortedMap
public static <K,V> BiCollector<K,V, toImmutableSortedMapcom.google.common.collect.ImmutableSortedMap<K, V>> (Comparator<? super K> comparator) Returns aBiCollector
that collects the key-value pairs into anImmutableSortedMap
according tocomparator
. -
toMultimap
public static <K,V, BiCollector<K,M extends com.google.common.collect.Multimap<K, V>> V, toMultimapM> (Supplier<M> multimapSupplier) Returns aBiCollector
that collects the key-value pairs into aMultimap
created withmultimapSupplier
. -
toImmutableListMultimap
public static <K,V> BiCollector<K,V, toImmutableListMultimap()com.google.common.collect.ImmutableListMultimap<K, V>> Returns aBiCollector
that collects the key-value pairs into anImmutableListMultimap
. Equivalent toImmutableListMultimap::toImmutableListMultimap
. -
flatteningToImmutableListMultimap
public static <K,T, BiCollector<K,V> T, flatteningToImmutableListMultimapcom.google.common.collect.ImmutableListMultimap<K, V>> (Function<? super T, ? extends Stream<? extends V>> flattener) Returns aBiCollector
that first flattens each value of the input pair withflattener
, and then collects the flattened pairs into anImmutableListMultimap
.For example, you can collect
groupingBy()
results into a multimap using:Map<PhoneNumber, Contact> phoneBook = ...; ImmutableListMultimap<AreaCode, Contact> contactsByAreaCode = BiStream.from(phoneBook) .collect(BiCollectors.groupingBy(PhoneNumber::areaCode, mergingContacts())) .collect(BiCollectors.flatteningToImmutableListMultimap(Collection::stream));
-
toImmutableSetMultimap
public static <K,V> BiCollector<K,V, toImmutableSetMultimap()com.google.common.collect.ImmutableSetMultimap<K, V>> Returns aBiCollector
that collects the key-value pairs into anImmutableSetMultimap
. Equivalent toImmutableSetMultimap::toImmutableSetMultimap
. -
flatteningToImmutableSetMultimap
public static <K,T, BiCollector<K,V> T, flatteningToImmutableSetMultimapcom.google.common.collect.ImmutableSetMultimap<K, V>> (Function<? super T, ? extends Stream<? extends V>> flattener) Returns aBiCollector
that first flattens each value of the input pair withflattener
, and then collects the flattened pairs into anImmutableSetMultimap
.For example, you can collect
groupingBy()
results into a multimap using:Map<PhoneNumber, Contact> phoneBook = ...; ImmutableSetMultimap<AreaCode, Contact> contactsByAreaCode = BiStream.from(phoneBook) .collect(BiCollectors.groupingBy(PhoneNumber::areaCode, mergingContacts())) .collect(BiCollectors.flatteningToImmutableSetMultimap(Collection::stream));
-
toImmutableMultiset
public static <K,V> BiCollector<K,V, toImmutableMultisetcom.google.common.collect.ImmutableMultiset<K>> (ToIntFunction<? super V> countFunction) Returns aBiCollector
that collects the key-value pairs into anImmutableMultiset
whose elements are the keys, with counts equal to the result of applyingcountFunction
to the values.For duplicate keys (according to
Object.equals(java.lang.Object)
), the first occurrence in encounter order appears in the resulting multiset, with count equal to the sum of the outputs ofcountFunction.applyAsInt(value)
for eachvalue
mapped to that key.biStream.collect(toImmutableMultiset(countFunction))
is logically equivalent tobiStream.collect(toImmutableMap(summingInt(countFunction)))
, except that it collects toImmutableMultiset<K>
while the latter collects toImmutableMap<K, Integer>
. -
toImmutableBiMap
public static <K,V> BiCollector<K,V, toImmutableBiMap()com.google.common.collect.ImmutableBiMap<K, V>> Returns aBiCollector
that collects the key-value pairs into anImmutableBiMap
. Equivalent toImmutableBimap::toImmutableBiMap
. -
toImmutableTable
public static <R,C, BiCollector<R,V> BiStream<? extends C, toImmutableTable()? extends V>, com.google.common.collect.ImmutableTable<R, C, V>> Returns aBiCollector
that collects the key-value pairs into ancom.google.common.collect.ImmutableTable<R,
, where each input key (of typeC, V> R
) is mapped to a row in the table, and each input value is aBiStream<C, V>
whose keys (of typeC
) are mapped to columns in the table, and whose values (of typeV
) are mapped to the cell values.Typically useful in combination with a nested
BiStream.groupingBy(java.util.function.Function<? super V, ? extends K>, java.util.function.BinaryOperator<V>)
. For example:import static com.google.common.labs.collect.BiStream.groupingBy; List<Contact> contacts = ...; ImmutableTable<LastName, FirstName, Long> nameCounts = contacts.stream() .collect(groupingBy(Contact::lastName, groupingBy(Contact::firstName, counting()))) .collect(toImmutableTable());
Similarly, cascading group-by can be performed on a
Map
orMultimap
throughgroupingBy()
, and then reduced in the same way usingtoImmutableTable()
:import static com.google.common.labs.collect.BiCollectors.groupingBy; Multimap<Address, PhoneNumber> phoneBook = ...; ImmutableTable<State, City, ImmutableSet<PhoneNumber>> phoneNumbersByLocation = BiStream.from(phoneBook) .collect(groupingBy(Address::state, groupingBy(Address::city, toImmutableSet()))) .collect(toImmutableTable());
Cells are collected in encounter order.
The returned
BiCollector
is not optimized for parallel reduction. -
toImmutableMap
public static <T,K, Collector<T,V> ?, toImmutableMapcom.google.common.collect.ImmutableMap<K, V>> (Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableMap
.- Since:
- 5.1
-
toImmutableListMultimap
public static <T,K, Collector<T,V> ?, toImmutableListMultimapcom.google.common.collect.ImmutableListMultimap<K, V>> (Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableListMultimap
.- Since:
- 5.1
-
toImmutableSetMultimap
public static <T,K, Collector<T,V> ?, toImmutableSetMultimapcom.google.common.collect.ImmutableSetMultimap<K, V>> (Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableSetMultimap
.- Since:
- 5.1
-
toImmutableBiMap
public static <T,K, Collector<T,V> ?, toImmutableBiMapcom.google.common.collect.ImmutableBiMap<K, V>> (Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableBiMap
.- Since:
- 5.1
-
toImmutableMapIgnoringDuplicateEntries
public static <T,K, Collector<T,V> ?, toImmutableMapIgnoringDuplicateEntriescom.google.common.collect.ImmutableMap<K, V>> (Function<? super T, ? extends Both<? extends K, ? extends V>> mapper) Returns a collector that first maps each input into a key-value pair, and then collects them into aImmutableListMultimap
.Inconsistent (unequal) values mapped to the same key (according to
Object.equals(Object)
) will throwIllegalArgumentException
, Duplicate (equal) values mapped to the same key will be ignored. Entries will appear in the encounter order of the first occurrence of the key.- Since:
- 6.6
-
toImmutableTable
public static <T,R, Collector<T,C, V> ?, toImmutableTablecom.google.common.collect.ImmutableTable<R, C, V>> (Function<? super T, ? extends R> rowFunction, Function<? super T, ? extends C> columnFunction, Collector<T, ?, V> cellCollector) Returns a collector that maps each value into a row-key and column-key for a table, and then collects all values mapped to the same cell usingcellCollector
. For example:ImmutableTable<State, County, ImmutableSet<City>> citiesByStateAndCounty = cities.stream().collect( toImmutableTable(City::state, City::county, toImmutableSet());
To transform values before they are collected, use
Collectors.mapping(java.util.function.Function<? super T, ? extends U>, java.util.stream.Collector<? super U, A, R>)
. For more complex operations on row- or column-keys, look atBiStream.groupingBy(java.util.function.Function<? super V, ? extends K>, java.util.function.BinaryOperator<V>)
. For collectors that throw or merge when values map to the same cell, seeImmutableTable.toImmutableTable(java.util.function.Function<? super T, ? extends R>, java.util.function.Function<? super T, ? extends C>, java.util.function.Function<? super T, ? extends V>)
.- Since:
- 6.6
-
toImmutableMapIgnoringDuplicateEntries
public static <T,K, Collector<T,V> ?, toImmutableMapIgnoringDuplicateEntriescom.google.common.collect.ImmutableMap<K, V>> (Function<? super T, ? extends K> toKey, Function<? super T, ? extends V> toValue) Returns aCollector
that accumulates elements into anImmutableMap
whose keys and values are the result of applying the provided mapping functions to the input elements.Inconsistent (unequal) values mapped to the same key (according to
Object.equals(Object)
) will throwIllegalArgumentException
, Duplicate (equal) values mapped to the same key will be ignored. Entries will appear in the encounter order of the first occurrence of the key.- Since:
- 6.6
-
partitioningBy
public static <T> Collector<T,?, partitioningByBoth<com.google.common.collect.ImmutableList<T>, com.google.common.collect.ImmutableList<T>>> (Predicate<? super T> predicate) Returns a collector that partitions the incoming elements into two groups: elements that matchpredicate
, and those that don't.For example:
candidates .collect(partitioningBy(Candidate::isEligible)) .andThen((eligible, ineligible) -> ...);
Null elements are not allowed. To support nulls, use
MoreCollectors.partitioningBy(java.util.function.Predicate<? super E>, java.util.stream.Collector<E, ?, ? extends R>)
with a null-supporting downstream collector, such asMoreCollectors.partitioningBy(predicate, toList())
.- Since:
- 6.0
-
toListOf
public static <F,T> Collector<F,?, toListOfcom.google.common.collect.ImmutableList<T>> (Function<? super F, ? extends T> mapper) Returns a collector that collects the results of applying themapper
function on the input elements into anImmutableList
.Equivalent to but more convenient than
Collectors.mapping(mapper, toImmutableList())
.- Since:
- 7.0
-
toSetOf
public static <F,T> Collector<F,?, toSetOfcom.google.common.collect.ImmutableSet<T>> (Function<? super F, ? extends T> mapper) Returns a collector that collects the results of applying themapper
function on the input elements into anImmutableSet
.Equivalent to but more convenient than
Collectors.mapping(mapper, toImmutableSet())
.- Since:
- 7.0
-
toImmutableRangeMap
public static <K extends Comparable<K>,V> BiCollector<com.google.common.collect.Range<K>,V, toImmutableRangeMap()com.google.common.collect.ImmutableRangeMap<K, V>> Returns a BiCollector that takes input (disjoint) ranges and the corresponding values and builds anImmutableRangeMap
.- Since:
- 8.1
-
toImmutableRangeMap
public static <K extends Comparable<K>,V> BiCollector<com.google.common.collect.Range<K>,V, toImmutableRangeMapcom.google.common.collect.ImmutableRangeMap<K, V>> (BinaryOperator<V> merger) Returns a BiCollector that merges values mapped to overlapping ranges using themerger
function and builds anImmutableRangeMap
with disjoint ranges and the corresponding merged values.For example:
Map<Range<Integer>, String> rangeMap = ...; // [1..3] -> "foo", [2..4] -> "bar" // [1..2) -> "foo", [2..3] -> "foobar", (3..4] -> "bar" ImmutableRangeMap<Integer, String> result = BiStream.from(rangeMap).collect(toImmutableRangeMap(String::concat));
To avoid quadratic range merging, it's generally safer to arrange the input ranges longer range first.
- Since:
- 8.1
-
toRangeMap
public static <K extends Comparable<K>,V, BiCollector<com.google.common.collect.Range<K>,M extends com.google.common.collect.RangeMap<K, V>> V, toRangeMapM> (Supplier<? extends M> factory, BinaryOperator<V> merger) Returns a BiCollector that merges values mapped to overlapping ranges using themerger
function and builds a mutableRangeMap
of typeM
that's created byfactory
, and populated with disjoint ranges and the corresponding merged values.For example:
Map<Range<Integer>, String> rangeMap = ...; // [1..3] -> "foo", [2..4] -> "bar" // [1..2) -> "foo", [2..3] -> "foobar", (3..4] -> "bar" TreeRangeMap<Integer, String> result = BiStream.from(rangeMap).collect(toRangeMap(TreeRangeMap::create, String::concat));
To avoid quadratic range merging, it's generally safer to arrange the input ranges longer range first.
- Since:
- 8.1
-
toDisjointRanges
public static <K extends Comparable<K>,V> BiCollector<com.google.common.collect.Range<K>,V, toDisjointRanges()BiStream<com.google.common.collect.Range<K>, Chain<V>>> Returns a BiCollector that merges values mapped to overlapping ranges into aChain
, which is an immutableList
that may have been deeplyconcatenated
. The result is aBiStream
with disjoint ranges and the corresponding merged list (Chain) of values.For example:
Map<Range<Integer>, String> rangeMap = ...; // [1..3] -> "foo", [2..4] -> "bar" // [1..2) -> ["foo"], [2..3] -> ["foo", "bar"], (3..4] -> ["bar"] ImmutableMap<Range<Integer>, List<String>> result = BiStream.from(rangeMap) .collect(toDisjointRanges()) .collect(toImmutableMap());
To avoid quadratic range merging, it's generally safer to arrange the input ranges longer range first.
- Since:
- 8.1
-
toDisjointRanges
public static <K extends Comparable<K>,V> BiCollector<com.google.common.collect.Range<K>,V, toDisjointRangesBiStream<com.google.common.collect.Range<K>, V>> (BinaryOperator<V> merger) Returns a BiCollector that merges values mapped to overlapping ranges using themerger
function and builds aBiStream
with disjoint ranges and the corresponding merged values.For example:
Map<Range<Integer>, String> rangeMap = ...; // [1..3] -> "foo", [2..4] -> "bar" // [1..2) -> "foo", [2..3] -> "foobar", (3..4] -> "bar" Map<Range<Integer>, String> result = BiStream.from(rangeMap) .collect(toDisjointRanges(String::concat)) .toMap();
To avoid quadratic range merging, it's generally safer to arrange the input ranges longer range first.
- Since:
- 8.1
-
toDisjointRanges
public static <K extends Comparable<K>,V, BiCollector<com.google.common.collect.Range<K>,R> V, toDisjointRangesBiStream<com.google.common.collect.Range<K>, R>> (Collector<V, ?, R> valueCollector) Returns a BiCollector that collects values mapped to overlapping ranges usingvalueCollector
, and builds aBiStream
with disjoint ranges and the corresponding collector results.For example:
Map<Range<Integer>, String> rangeMap = ...; // [1..3] -> foo, [2..4] -> bar // [1..2) -> [foo], [2..3] -> [foo, bar], (3..4] -> [bar] Map<Range<Integer>, ImmutableSet<String>> result = BiStream.from(rangeMap) .collect(toDisjointRanges(toImmutableSet())) .toMap();
Performance note: when a range is repetitively detected to overlap with another range, and the mapped values are re-mapped to each split sub-range, the re-mapping takes O(1) time even when there have been N mapped values. The final mapped values for each disjoint range will be eventually collected using the
valueCollector
, once per disjoint range. In other words, ifvalueCollector
is O(n), toDisjointRanges() is an O(nlogn) operation, while the worst-case runtime of a naive implementation that recombines the mapped values on each re-mapping will be O(n^2).Also, to avoid quadratic range merging, it's generally safer to arrange the input ranges longer range first.
- Since:
- 8.1
-