Class Multisets
Multiset instances.
See the Guava User Guide article on
Multisets.
- Since:
- 2.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classprivate static final class(package private) static class(package private) static class(package private) static final class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static <E> booleanaddAllImpl(Multiset<E> self, Collection<? extends E> elements) An implementation ofCollection.addAll(java.util.Collection<? extends E>).private static <E> booleanaddAllImpl(Multiset<E> self, Multiset<? extends E> elements) A specialization ofaddAllImplfor whenelementsis itself a Multiset.(package private) static <T> Multiset<T>Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557static booleancontainsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset) ReturnstrueifsubMultiset.count(o) <= superMultiset.count(o)for allo.(package private) static booleanequalsImpl(Multiset<?> multiset, @Nullable Object object) An implementation ofMultiset.equals(java.lang.Object).(package private) static <E> Iterator<E>iteratorImpl(Multiset<E> multiset) An implementation ofMultiset.iterator().(package private) static intlinearTimeSizeImpl(Multiset<?> multiset) An implementation ofMultiset.size().(package private) static booleanremoveAllImpl(Multiset<?> self, Collection<?> elementsToRemove) An implementation ofMultiset.removeAll(java.util.Collection<?>).static booleanremoveOccurrences(Multiset<?> multisetToModify, 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.(package private) static booleanretainAllImpl(Multiset<?> self, Collection<?> elementsToRetain) An implementation ofMultiset.retainAll(java.util.Collection<?>).static booleanretainOccurrences(Multiset<?> multisetToModify, Multiset<?> multisetToRetain) ModifiesmultisetToModifyso that its count for an elementeis at mostmultisetToRetain.count(e).private static <E> booleanretainOccurrencesImpl(Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain) Delegate implementation which cares about the element type.(package private) static <E> Spliterator<E>spliteratorImpl(Multiset<E> multiset) toMultiset(Function<? super T, E> elementFunction, ToIntFunction<? super T> countFunction, 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.
-
Constructor Details
-
Multisets
private Multisets()
-
-
Method Details
-
toMultiset
public static <T,E, Collector<T,M extends Multiset<E>> ?, toMultisetM> (Function<? super T, E> elementFunction, ToIntFunction<? super T> countFunction, 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
-
retainOccurrencesImpl
private static <E> boolean retainOccurrencesImpl(Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain) Delegate implementation which cares about the element type. -
removeOccurrences
@CanIgnoreReturnValue public static boolean removeOccurrences(Multiset<?> multisetToModify, 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)
-
equalsImpl
An implementation ofMultiset.equals(java.lang.Object). -
addAllImpl
An implementation ofCollection.addAll(java.util.Collection<? extends E>). -
addAllImpl
A specialization ofaddAllImplfor whenelementsis itself a Multiset. -
removeAllImpl
An implementation ofMultiset.removeAll(java.util.Collection<?>). -
retainAllImpl
An implementation ofMultiset.retainAll(java.util.Collection<?>). -
iteratorImpl
An implementation ofMultiset.iterator(). -
spliteratorImpl
-
linearTimeSizeImpl
An implementation ofMultiset.size(). -
cast
Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
-