/** * Returns a {@code Collector} that accumulates elements into a multiset created via the specified * {@code Supplier}, whose elements are the result of applying {@code elementFunction} to the * inputs, with counts equal to the result of applying {@code countFunction} to the inputs. * Elements are added in encounter order. * * <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the element * will be added more than once, with the count summed over all appearances of the element. * * <p>Note that {@code stream.collect(toMultiset(function, e -> 1, supplier))} is equivalent to * {@code stream.map(function).collect(Collectors.toCollection(supplier))}. * * @since 22.0 */ public static <T, E, M extends Multiset<E>> Collector<T, ?, M> toMultiset( java.util.function.Function<T, E> elementFunction, java.util.function.ToIntFunction<T> countFunction, java.util.function.Supplier<M> multisetSupplier) { checkNotNull(elementFunction); checkNotNull(countFunction); checkNotNull(multisetSupplier); return Collector.of( multisetSupplier, (ms, t) -> ms.add(elementFunction.apply(t), countFunction.applyAsInt(t)), (ms1, ms2) -> { ms1.addAll(ms2); return ms1; }); }
/** * Returns a {@code Collector} that accumulates elements into an {@code ImmutableMultiset} whose * elements are the result of applying {@code elementFunction} to the inputs, with counts equal to * the result of applying {@code countFunction} to the inputs. * * <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the first * occurrence in encounter order appears in the resulting multiset, with count equal to the sum of * the outputs of {@code countFunction.applyAsInt(t)} for each {@code t} mapped to that element. * * @since 22.0 */ public static <T, E> Collector<T, ?, ImmutableMultiset<E>> toImmutableMultiset( Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) { checkNotNull(elementFunction); checkNotNull(countFunction); return Collector.of( LinkedHashMultiset::create, (multiset, t) -> multiset.add(checkNotNull(elementFunction.apply(t)), countFunction.applyAsInt(t)), (multiset1, multiset2) -> { multiset1.addAll(multiset2); return multiset1; }, (Multiset<E> multiset) -> copyFromEntries(multiset.entrySet())); }
/** * Returns a {@code Collector} that accumulates elements into an {@code ImmutableSortedMultiset} * whose elements are the result of applying {@code elementFunction} to the inputs, with counts * equal to the result of applying {@code countFunction} to the inputs. * * <p>If the mapped elements contain duplicates (according to {@code comparator}), the first * occurrence in encounter order appears in the resulting multiset, with count equal to the sum of * the outputs of {@code countFunction.applyAsInt(t)} for each {@code t} mapped to that element. * * @since 22.0 */ public static <T, E> Collector<T, ?, ImmutableSortedMultiset<E>> toImmutableSortedMultiset( Comparator<? super E> comparator, Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) { checkNotNull(comparator); checkNotNull(elementFunction); checkNotNull(countFunction); return Collector.of( () -> TreeMultiset.create(comparator), (multiset, t) -> multiset.add(checkNotNull(elementFunction.apply(t)), countFunction.applyAsInt(t)), (multiset1, multiset2) -> { multiset1.addAll(multiset2); return multiset1; }, (Multiset<E> multiset) -> copyOfSortedEntries(comparator, multiset.entrySet())); }