Package com.google.common.collect
Class EnumMultiset<E extends Enum<E>>
java.lang.Object
java.util.AbstractCollection<E>
com.google.common.collect.AbstractMultiset<E>
com.google.common.collect.EnumMultiset<E>
- All Implemented Interfaces:
Multiset<E>,Serializable,Iterable<E>,Collection<E>
public final class EnumMultiset<E extends Enum<E>>
extends AbstractMultiset<E>
implements Serializable
Multiset implementation specialized for enum elements, supporting all single-element operations
in O(1).
See the Guava User Guide article on
Multiset.
- Since:
- 2.0
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.google.common.collect.AbstractMultiset
AbstractMultiset.ElementSet, AbstractMultiset.EntrySetNested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int[]private intprivate E[]private static final longprivate long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintAdds a number of occurrences of an element to this multiset.private voidReturnselementcast toE, if it actually is a nonnull E.voidclear()intReturns the number of occurrences of an element in this multiset (the count of the element).static <E extends Enum<E>>
EnumMultiset<E>Creates an emptyEnumMultiset.static <E extends Enum<E>>
EnumMultiset<E>Creates a newEnumMultisetcontaining the specified elements.static <E extends Enum<E>>
EnumMultiset<E>Returns a newEnumMultisetinstance containing the given elements.(package private) int(package private) Iterator<Multiset.Entry<E>>voidforEachEntry(ObjIntConsumer<? super E> action) Runs the specified action for each distinct element in this multiset, and the number of occurrences of that element.private booleaniterator()private voidreadObject(ObjectInputStream stream) intRemoves a number of occurrences of the specified element from this multiset.intAdds or removes the necessary occurrences of an element such that the element attains the desired count.intsize()Returns the total number of all occurrences of all elements in this multiset.private voidwriteObject(ObjectOutputStream stream) Methods inherited from class com.google.common.collect.AbstractMultiset
add, addAll, contains, createElementSet, createEntrySet, elementSet, entrySet, equals, hashCode, isEmpty, remove, removeAll, retainAll, setCount, toStringMethods inherited from class java.util.AbstractCollection
containsAll, toArray, toArrayMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray, toArray, toArrayMethods inherited from interface com.google.common.collect.Multiset
containsAll, forEach, spliterator
-
Field Details
-
type
-
enumConstants
-
counts
private transient int[] counts -
distinctElements
private transient int distinctElements -
size
private transient long size -
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
EnumMultiset
Creates an emptyEnumMultiset.
-
-
Method Details
-
create
Creates an emptyEnumMultiset. -
create
Creates a newEnumMultisetcontaining the specified elements.This implementation is highly efficient when
elementsis itself aMultiset.- Parameters:
elements- the elements that the multiset should contain- Throws:
IllegalArgumentException- ifelementsis empty
-
create
Returns a newEnumMultisetinstance containing the given elements. Unlikecreate(Iterable), this method does not produce an exception on an empty iterable.- Since:
- 14.0
-
isActuallyE
-
checkIsE
Returnselementcast toE, if it actually is a nonnull E. Otherwise, throws either a NullPointerException or a ClassCastException as appropriate. -
distinctElements
int distinctElements()- Specified by:
distinctElementsin classAbstractMultiset<E extends Enum<E>>
-
size
public int size()Description copied from interface:MultisetReturns the total number of all occurrences of all elements in this multiset.Note: this method does not return the number of distinct elements in the multiset, which is given by
entrySet().size(). -
count
Description copied from interface:MultisetReturns the number of occurrences of an element in this multiset (the count of the element). Note that for anObject.equals(java.lang.Object)-based multiset, this gives the same result asCollections.frequency(java.util.Collection<?>, java.lang.Object)(which would presumably perform more poorly).Note: the utility method
Iterables.frequency(java.lang.Iterable<?>, java.lang.Object)generalizes this operation; it correctly delegates to this method when dealing with a multiset, but it can also accept any other iterable type. -
add
Description copied from interface:MultisetAdds a number of occurrences of an element to this multiset. Note that ifoccurrences == 1, this method has the identical effect toMultiset.add(Object). This method is functionally equivalent (except in the case of overflow) to the calladdAll(Collections.nCopies(element, occurrences)), which would presumably perform much more poorly.- Specified by:
addin interfaceMultiset<E extends Enum<E>>- Overrides:
addin classAbstractMultiset<E extends Enum<E>>- Parameters:
element- the element to add occurrences of; may be null only if explicitly allowed by the implementationoccurrences- the number of occurrences of the element to add. May be zero, in which case no change will be made.- Returns:
- the count of the element before the operation; possibly zero
-
remove
Description copied from interface:MultisetRemoves a number of occurrences of the specified element from this multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed. Note that ifoccurrences == 1, this is functionally equivalent to the callremove(element).- Specified by:
removein interfaceMultiset<E extends Enum<E>>- Overrides:
removein classAbstractMultiset<E extends Enum<E>>- Parameters:
element- the element to conditionally remove occurrences ofoccurrences- the number of occurrences of the element to remove. May be zero, in which case no change will be made.- Returns:
- the count of the element before the operation; possibly zero
-
setCount
Description copied from interface:MultisetAdds or removes the necessary occurrences of an element such that the element attains the desired count.- Specified by:
setCountin interfaceMultiset<E extends Enum<E>>- Overrides:
setCountin classAbstractMultiset<E extends Enum<E>>- Parameters:
element- the element to add or remove occurrences of; may be null only if explicitly allowed by the implementationcount- the desired count of the element in this multiset- Returns:
- the count of the element before the operation; possibly zero
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E extends Enum<E>>- Specified by:
clearin classAbstractMultiset<E extends Enum<E>>
-
elementIterator
- Specified by:
elementIteratorin classAbstractMultiset<E extends Enum<E>>
-
entryIterator
Iterator<Multiset.Entry<E>> entryIterator()- Specified by:
entryIteratorin classAbstractMultiset<E extends Enum<E>>
-
forEachEntry
Description copied from interface:MultisetRuns the specified action for each distinct element in this multiset, and the number of occurrences of that element. For someMultisetimplementations, this may be more efficient than iterating over theMultiset.entrySet()either explicitly or withentrySet().forEach(action).- Specified by:
forEachEntryin interfaceMultiset<E extends Enum<E>>
-
iterator
Description copied from interface:MultisetElements that occur multiple times in the multiset will appear multiple times in this iterator, though not necessarily sequentially.
-
writeObject
- Throws:
IOException
-
readObject
- Throws:
IOExceptionClassNotFoundException
-