Modifier and Type | Class and Description |
---|---|
(package private) static class |
Multisets.AbstractEntry<E>
|
private static class |
Multisets.DecreasingCount |
(package private) static class |
Multisets.ElementSet<E> |
(package private) static class |
Multisets.EntrySet<E> |
private static class |
Multisets.FilteredMultiset<E> |
(package private) static class |
Multisets.ImmutableEntry<E> |
(package private) static class |
Multisets.MultisetIteratorImpl<E> |
(package private) static class |
Multisets.UnmodifiableMultiset<E> |
private static class |
Multisets.ViewMultiset<E>
An
AbstractMultiset with additional default implementations, some of them linear-time
implementations in terms of elementSet and entrySet . |
Modifier | Constructor and Description |
---|---|
private |
Multisets() |
Modifier and Type | Method and Description |
---|---|
(package private) static <E> boolean |
addAllImpl(Multiset<E> self,
java.util.Collection<? extends E> elements)
An implementation of
Collection.addAll(java.util.Collection<? extends E>) . |
private static <E> boolean |
addAllImpl(Multiset<E> self,
Multiset<? extends E> elements)
A specialization of
addAllImpl for when elements is itself a Multiset. |
(package private) static <T> Multiset<T> |
cast(java.lang.Iterable<T> iterable)
Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
|
static boolean |
containsOccurrences(Multiset<?> superMultiset,
Multiset<?> subMultiset)
Returns
true if subMultiset.count(o) <= superMultiset.count(o) for all o . |
static <E> ImmutableMultiset<E> |
copyHighestCountFirst(Multiset<E> multiset)
Returns a copy of
multiset as an ImmutableMultiset whose iteration order is
highest count first, with ties broken by the iteration order of the original multiset. |
static <E> Multiset<E> |
difference(Multiset<E> multiset1,
Multiset<?> multiset2)
Returns an unmodifiable view of the difference of two multisets.
|
(package private) static <E> java.util.Iterator<E> |
elementIterator(java.util.Iterator<Multiset.Entry<E>> entryIterator) |
(package private) static boolean |
equalsImpl(Multiset<?> multiset,
java.lang.Object object)
An implementation of
Multiset.equals(java.lang.Object) . |
static <E> Multiset<E> |
filter(Multiset<E> unfiltered,
Predicate<? super E> predicate)
Returns a view of the elements of
unfiltered that satisfy a predicate. |
static <E> Multiset.Entry<E> |
immutableEntry(E e,
int n)
Returns an immutable multiset entry with the specified element and count.
|
(package private) static int |
inferDistinctElements(java.lang.Iterable<?> elements)
Returns the expected number of distinct elements given the specified elements.
|
static <E> Multiset<E> |
intersection(Multiset<E> multiset1,
Multiset<?> multiset2)
Returns an unmodifiable view of the intersection of two multisets.
|
(package private) static <E> java.util.Iterator<E> |
iteratorImpl(Multiset<E> multiset)
An implementation of
Multiset.iterator() . |
(package private) static int |
linearTimeSizeImpl(Multiset<?> multiset)
An implementation of
Multiset.size() . |
(package private) static boolean |
removeAllImpl(Multiset<?> self,
java.util.Collection<?> elementsToRemove)
An implementation of
Multiset.removeAll(java.util.Collection<?>) . |
static boolean |
removeOccurrences(Multiset<?> multisetToModify,
java.lang.Iterable<?> occurrencesToRemove)
For each occurrence of an element
e in occurrencesToRemove , removes one
occurrence of e in multisetToModify . |
static boolean |
removeOccurrences(Multiset<?> multisetToModify,
Multiset<?> occurrencesToRemove)
For each occurrence of an element
e in occurrencesToRemove , removes one
occurrence of e in multisetToModify . |
(package private) static boolean |
retainAllImpl(Multiset<?> self,
java.util.Collection<?> elementsToRetain)
An implementation of
Multiset.retainAll(java.util.Collection<?>) . |
static boolean |
retainOccurrences(Multiset<?> multisetToModify,
Multiset<?> multisetToRetain)
Modifies
multisetToModify so that its count for an element e is at most multisetToRetain.count(e) . |
private static <E> boolean |
retainOccurrencesImpl(Multiset<E> multisetToModify,
Multiset<?> occurrencesToRetain)
Delegate implementation which cares about the element type.
|
(package private) static <E> int |
setCountImpl(Multiset<E> self,
E element,
int count)
An implementation of
Multiset.setCount(Object, int) . |
(package private) static <E> boolean |
setCountImpl(Multiset<E> self,
E element,
int oldCount,
int newCount)
An implementation of
Multiset.setCount(Object, int, int) . |
(package private) static <E> java.util.Spliterator<E> |
spliteratorImpl(Multiset<E> multiset) |
static <E> Multiset<E> |
sum(Multiset<? extends E> multiset1,
Multiset<? extends E> multiset2)
Returns an unmodifiable view of the sum of two multisets.
|
static <T,E,M extends Multiset<E>> |
toMultiset(java.util.function.Function<? super T,E> elementFunction,
java.util.function.ToIntFunction<? super T> countFunction,
java.util.function.Supplier<M> multisetSupplier)
Returns a
Collector that accumulates elements into a multiset created via the specified
Supplier , whose elements are the result of applying elementFunction to the
inputs, with counts equal to the result of applying countFunction to the inputs. |
static <E> Multiset<E> |
union(Multiset<? extends E> multiset1,
Multiset<? extends E> multiset2)
Returns an unmodifiable view of the union of two multisets.
|
static <E> Multiset<E> |
unmodifiableMultiset(ImmutableMultiset<E> multiset)
Deprecated.
no need to use this
|
static <E> Multiset<E> |
unmodifiableMultiset(Multiset<? extends E> multiset)
Returns an unmodifiable view of the specified multiset.
|
static <E> SortedMultiset<E> |
unmodifiableSortedMultiset(SortedMultiset<E> sortedMultiset)
Returns an unmodifiable view of the specified sorted multiset.
|
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)
Collector
that accumulates elements into a multiset created via the specified
Supplier
, whose elements are the result of applying elementFunction
to the
inputs, with counts equal to the result of applying countFunction
to 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 to
stream.map(function).collect(Collectors.toCollection(supplier))
.
To collect to an ImmutableMultiset
, use ImmutableMultiset.toImmutableMultiset()
.
public static <E> Multiset<E> unmodifiableMultiset(Multiset<? extends E> multiset)
UnsupportedOperationException
.
The returned multiset will be serializable if the specified multiset is serializable.
multiset
- the multiset for which an unmodifiable view is to be generated@Deprecated public static <E> Multiset<E> unmodifiableMultiset(ImmutableMultiset<E> multiset)
public static <E> SortedMultiset<E> unmodifiableSortedMultiset(SortedMultiset<E> sortedMultiset)
UnsupportedOperationException
.
The returned multiset will be serializable if the specified multiset is serializable.
sortedMultiset
- the sorted multiset for which an unmodifiable view is to be generatedpublic static <E> Multiset.Entry<E> immutableEntry(E e, int n)
e
is.e
- the element to be associated with the returned entryn
- the count to be associated with the returned entryjava.lang.IllegalArgumentException
- if n
is negativepublic static <E> Multiset<E> filter(Multiset<E> unfiltered, Predicate<? super E> predicate)
unfiltered
that satisfy a predicate. The returned
multiset is a live view of unfiltered
; changes to one affect the other.
The resulting multiset's iterators, and those of its entrySet()
and elementSet()
, do not support remove()
. However, all other multiset methods supported
by unfiltered
are supported by the returned multiset. When given an element that
doesn't satisfy the predicate, the multiset's add()
and addAll()
methods throw
an IllegalArgumentException
. When methods such as removeAll()
and clear()
are called on the filtered multiset, only elements that satisfy the filter will be
removed from the underlying multiset.
The returned multiset isn't threadsafe or serializable, even if unfiltered
is.
Many of the filtered multiset's methods, such as size()
, iterate across every
element in the underlying multiset and determine which elements satisfy the filter. When a live
view is not needed, it may be faster to copy the returned multiset and use the copy.
Warning: predicate
must be consistent with equals, as documented at
Predicate.apply(T)
. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class)
, which is inconsistent with equals. (See Iterables.filter(Iterable, Class)
for related functionality.)
static int inferDistinctElements(java.lang.Iterable<?> elements)
elements
is an instance of Multiset
;
otherwise the default value of 11 is returned.public static <E> Multiset<E> union(Multiset<? extends E> multiset1, Multiset<? extends E> multiset2)
multiset1
followed by
the members of the element set of multiset2
that are not contained in multiset1
, with repeated occurrences of the same element appearing consecutively.
Results are undefined if multiset1
and multiset2
are based on different
equivalence relations (as HashMultiset
and TreeMultiset
are).
public static <E> Multiset<E> intersection(Multiset<E> multiset1, Multiset<?> multiset2)
multiset1
, with repeated occurrences of the
same element appearing consecutively.
Results are undefined if multiset1
and multiset2
are based on different
equivalence relations (as HashMultiset
and TreeMultiset
are).
public static <E> Multiset<E> sum(Multiset<? extends E> multiset1, Multiset<? extends E> multiset2)
multiset1
followed by
the members of the element set of multiset2
that are not contained in multiset1
, with repeated occurrences of the same element appearing consecutively.
Results are undefined if multiset1
and multiset2
are based on different
equivalence relations (as HashMultiset
and TreeMultiset
are).
public static <E> Multiset<E> difference(Multiset<E> multiset1, Multiset<?> multiset2)
multiset1
, with repeated occurrences of the same element appearing consecutively.
Results are undefined if multiset1
and multiset2
are based on different
equivalence relations (as HashMultiset
and TreeMultiset
are).
public static boolean containsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset)
true
if subMultiset.count(o) <= superMultiset.count(o)
for all o
.public static boolean retainOccurrences(Multiset<?> multisetToModify, Multiset<?> multisetToRetain)
multisetToModify
so that its count for an element e
is at most multisetToRetain.count(e)
.
To be precise, multisetToModify.count(e)
is set to Math.min(multisetToModify.count(e), multisetToRetain.count(e))
. This is similar to intersection
(multisetToModify, multisetToRetain)
,
but mutates multisetToModify
instead of returning a view.
In contrast, multisetToModify.retainAll(multisetToRetain)
keeps all occurrences of
elements that appear at all in multisetToRetain
, and deletes all occurrences of all
other elements.
true
if multisetToModify
was changed as a result of this operationprivate static <E> boolean retainOccurrencesImpl(Multiset<E> multisetToModify, Multiset<?> occurrencesToRetain)
public static boolean removeOccurrences(Multiset<?> multisetToModify, java.lang.Iterable<?> occurrencesToRemove)
e
in occurrencesToRemove
, removes one
occurrence of e
in multisetToModify
.
Equivalently, this method modifies multisetToModify
so that multisetToModify.count(e)
is set to Math.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
in occurrencesToRemove
. However, this operation is equivalent to, albeit
sometimes more efficient than, the following:
for (E e : occurrencesToRemove) {
multisetToModify.remove(e);
}
true
if multisetToModify
was changed as a result of this operationMultiset
)public static boolean removeOccurrences(Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove)
e
in occurrencesToRemove
, removes one
occurrence of e
in multisetToModify
.
Equivalently, this method modifies multisetToModify
so that multisetToModify.count(e)
is set to Math.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
in occurrencesToRemove
. However, this operation is equivalent to, albeit
sometimes more efficient than, the following:
for (E e : occurrencesToRemove) {
multisetToModify.remove(e);
}
true
if multisetToModify
was changed as a result of this operationIterable
was present)static boolean equalsImpl(Multiset<?> multiset, @CheckForNull java.lang.Object object)
Multiset.equals(java.lang.Object)
.static <E> boolean addAllImpl(Multiset<E> self, java.util.Collection<? extends E> elements)
Collection.addAll(java.util.Collection<? extends E>)
.private static <E> boolean addAllImpl(Multiset<E> self, Multiset<? extends E> elements)
addAllImpl
for when elements
is itself a Multiset.static boolean removeAllImpl(Multiset<?> self, java.util.Collection<?> elementsToRemove)
Multiset.removeAll(java.util.Collection<?>)
.static boolean retainAllImpl(Multiset<?> self, java.util.Collection<?> elementsToRetain)
Multiset.retainAll(java.util.Collection<?>)
.static <E> int setCountImpl(Multiset<E> self, E element, int count)
Multiset.setCount(Object, int)
.static <E> boolean setCountImpl(Multiset<E> self, E element, int oldCount, int newCount)
Multiset.setCount(Object, int, int)
.static <E> java.util.Iterator<E> elementIterator(java.util.Iterator<Multiset.Entry<E>> entryIterator)
static <E> java.util.Iterator<E> iteratorImpl(Multiset<E> multiset)
Multiset.iterator()
.static <E> java.util.Spliterator<E> spliteratorImpl(Multiset<E> multiset)
static int linearTimeSizeImpl(Multiset<?> multiset)
Multiset.size()
.static <T> Multiset<T> cast(java.lang.Iterable<T> iterable)
public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset)
multiset
as an ImmutableMultiset
whose iteration order is
highest count first, with ties broken by the iteration order of the original multiset.