Class Multisets
@GwtCompatible public final class Multisets extends java.lang.Object
Multiset instances.
See the Guava User Guide article on
Multisets.
- Since:
- 2.0
- Author:
- Kevin Bourrillion, Mike Bostock, Louis Wasserman
-
Method Summary
Modifier and Type Method Description static booleancontainsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset)ReturnstrueifsubMultiset.count(o) <= superMultiset.count(o)for allo.static booleanremoveOccurrences(Multiset<?> multisetToModify, java.lang.Iterable<?> occurrencesToRemove)For each occurrence of an elementeinoccurrencesToRemove, removes one occurrence ofeinmultisetToModify.static booleanremoveOccurrences(Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove)For each occurrence of an elementeinoccurrencesToRemove, removes one occurrence ofeinmultisetToModify.static booleanretainOccurrences(Multiset<?> multisetToModify, Multiset<?> multisetToRetain)ModifiesmultisetToModifyso that its count for an elementeis at mostmultisetToRetain.count(e).static <T, E, M extends Multiset<E>>
java.util.stream.Collector<T,?,M>toMultiset(java.util.function.Function<? super T,E> elementFunction, java.util.function.ToIntFunction<? super T> countFunction, java.util.function.Supplier<M> multisetSupplier)Returns aCollectorthat accumulates elements into a multiset created via the specifiedSupplier, whose elements are the result of applyingelementFunctionto the inputs, with counts equal to the result of applyingcountFunctionto the inputs.
-
Method Details
-
toMultiset
public static <T, E, M extends Multiset<E>> java.util.stream.Collector<T,?,M> toMultiset(java.util.function.Function<? super T,E> elementFunction, java.util.function.ToIntFunction<? super T> countFunction, java.util.function.Supplier<M> multisetSupplier)Returns aCollectorthat accumulates elements into a multiset created via the specifiedSupplier, whose elements are the result of applyingelementFunctionto the inputs, with counts equal to the result of applyingcountFunctionto the inputs. Elements are added in encounter order.If the mapped elements contain duplicates (according to
Object.equals(java.lang.Object)), the element will be added more than once, with the count summed over all appearances of the element.Note that
stream.collect(toMultiset(function, e -> 1, supplier))is equivalent tostream.map(function).collect(Collectors.toCollection(supplier)).- Since:
- 22.0
-
containsOccurrences
@CanIgnoreReturnValue public static boolean containsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset)ReturnstrueifsubMultiset.count(o) <= superMultiset.count(o)for allo.- Since:
- 10.0
-
retainOccurrences
@CanIgnoreReturnValue public static boolean retainOccurrences(Multiset<?> multisetToModify, Multiset<?> multisetToRetain)ModifiesmultisetToModifyso that its count for an elementeis at mostmultisetToRetain.count(e).To be precise,
multisetToModify.count(e)is set toMath.min(multisetToModify.count(e), multisetToRetain.count(e)). This is similar tointersection(multisetToModify, multisetToRetain), but mutatesmultisetToModifyinstead of returning a view.In contrast,
multisetToModify.retainAll(multisetToRetain)keeps all occurrences of elements that appear at all inmultisetToRetain, and deletes all occurrences of all other elements.- Returns:
trueifmultisetToModifywas changed as a result of this operation- Since:
- 10.0
-
removeOccurrences
@CanIgnoreReturnValue public static boolean removeOccurrences(Multiset<?> multisetToModify, java.lang.Iterable<?> occurrencesToRemove)For each occurrence of an elementeinoccurrencesToRemove, removes one occurrence ofeinmultisetToModify.Equivalently, this method modifies
multisetToModifyso thatmultisetToModify.count(e)is set toMath.max(0, multisetToModify.count(e) - Iterables.frequency(occurrencesToRemove, e)).This is not the same as
multisetToModify.removeAll(occurrencesToRemove), which removes all occurrences of elements that appear inoccurrencesToRemove. However, this operation is equivalent to, albeit sometimes more efficient than, the following:for (E e : occurrencesToRemove) { multisetToModify.remove(e); }- Returns:
trueifmultisetToModifywas changed as a result of this operation- Since:
- 18.0 (present in 10.0 with a requirement that the second parameter be a
Multiset)
-
removeOccurrences
@CanIgnoreReturnValue public static boolean removeOccurrences(Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove)For each occurrence of an elementeinoccurrencesToRemove, removes one occurrence ofeinmultisetToModify.Equivalently, this method modifies
multisetToModifyso thatmultisetToModify.count(e)is set toMath.max(0, multisetToModify.count(e) - occurrencesToRemove.count(e)).This is not the same as
multisetToModify.removeAll(occurrencesToRemove), which removes all occurrences of elements that appear inoccurrencesToRemove. However, this operation is equivalent to, albeit sometimes more efficient than, the following:for (E e : occurrencesToRemove) { multisetToModify.remove(e); }- Returns:
trueifmultisetToModifywas changed as a result of this operation- Since:
- 10.0 (missing in 18.0 when only the overload taking an
Iterablewas present)
-