Class ImmutableMultiset<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- com.google.common.collect.ImmutableCollection<E>
-
- com.google.common.collect.ImmutableMultiset<E>
-
- All Implemented Interfaces:
Multiset<E>
,Serializable
,Iterable<E>
,Collection<E>
- Direct Known Subclasses:
ImmutableSortedMultiset
@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableMultiset<E> extends ImmutableCollection<E> implements Multiset<E>
AMultiset
whose contents will never change, with many other important properties detailed atImmutableCollection
.Grouped iteration. In all current implementations, duplicate elements always appear consecutively when iterating. Elements iterate in order by the first appearance of that element when the multiset was created.
See the Guava User Guide article on immutable collections.
- Since:
- 2.0
- Author:
- Jared Levy, Louis Wasserman
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ImmutableMultiset.Builder<E>
A builder for creating immutable multiset instances, especiallypublic static final
multisets ("constant multisets").-
Nested classes/interfaces inherited from interface com.google.common.collect.Multiset
Multiset.Entry<E extends @Nullable Object>
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
add(E element, int occurrences)
Deprecated.Unsupported operation.ImmutableList<E>
asList()
Returns anImmutableList
containing the same elements, in the same order, as this collection.static <E> ImmutableMultiset.Builder<E>
builder()
Returns a new builder.boolean
contains(Object object)
Returnstrue
if this collection contains the specified element.static <E> ImmutableMultiset<E>
copyOf(E[] elements)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.static <E> ImmutableMultiset<E>
copyOf(Iterable<? extends E> elements)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.static <E> ImmutableMultiset<E>
copyOf(Iterator<? extends E> elements)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.abstract ImmutableSet<E>
elementSet()
Returns the set of distinct elements contained in this multiset.ImmutableSet<Multiset.Entry<E>>
entrySet()
Returns a view of the contents of this multiset, grouped intoMultiset.Entry
instances, each providing an element of the multiset and the count of that element.boolean
equals(Object object)
Indicates whether some other object is "equal to" this one.int
hashCode()
Returns a hash code value for the object.UnmodifiableIterator<E>
iterator()
Returns an unmodifiable iterator across the elements in this collection.static <E> ImmutableMultiset<E>
of()
Returns the empty immutable multiset.static <E> ImmutableMultiset<E>
of(E element)
Returns an immutable multiset containing a single element.static <E> ImmutableMultiset<E>
of(E e1, E e2)
Returns an immutable multiset containing the given elements, in order.static <E> ImmutableMultiset<E>
of(E e1, E e2, E e3)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.static <E> ImmutableMultiset<E>
of(E e1, E e2, E e3, E e4)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.static <E> ImmutableMultiset<E>
of(E e1, E e2, E e3, E e4, E e5)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.static <E> ImmutableMultiset<E>
of(E e1, E e2, E e3, E e4, E e5, E e6, E... others)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.int
remove(Object element, int occurrences)
Deprecated.Unsupported operation.int
setCount(E element, int count)
Deprecated.Unsupported operation.boolean
setCount(E element, int oldCount, int newCount)
Deprecated.Unsupported operation.static <E> Collector<E,?,ImmutableMultiset<E>>
toImmutableMultiset()
Returns aCollector
that accumulates the input elements into a newImmutableMultiset
.static <T extends @Nullable Object,E>
Collector<T,?,ImmutableMultiset<E>>toImmutableMultiset(Function<? super T,? extends E> elementFunction, ToIntFunction<? super T> countFunction)
Returns aCollector
that accumulates elements into anImmutableMultiset
whose elements are the result of applyingelementFunction
to the inputs, with counts equal to the result of applyingcountFunction
to the inputs.String
toString()
Returns a string representation of this collection.-
Methods inherited from class com.google.common.collect.ImmutableCollection
add, addAll, clear, remove, removeAll, removeIf, retainAll, spliterator, toArray, toArray
-
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, size
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
addAll, clear, isEmpty, parallelStream, removeIf, stream, toArray, toArray, toArray
-
Methods inherited from interface com.google.common.collect.Multiset
add, containsAll, count, forEach, forEachEntry, remove, removeAll, retainAll, size, spliterator
-
-
-
-
Method Detail
-
toImmutableMultiset
public static <E> Collector<E,?,ImmutableMultiset<E>> toImmutableMultiset()
Returns aCollector
that accumulates the input elements into a newImmutableMultiset
. Elements iterate in order by the first appearance of that element in encounter order.- Since:
- 21.0
-
toImmutableMultiset
public static <T extends @Nullable Object,E> Collector<T,?,ImmutableMultiset<E>> toImmutableMultiset(Function<? super T,? extends E> elementFunction, ToIntFunction<? super T> countFunction)
Returns aCollector
that accumulates elements into anImmutableMultiset
whose elements are the result of applyingelementFunction
to the inputs, with counts equal to the result of applyingcountFunction
to the inputs.If the mapped elements contain duplicates (according to
Object.equals(java.lang.Object)
), the first occurrence in encounter order appears in the resulting multiset, with count equal to the sum of the outputs ofcountFunction.applyAsInt(t)
for eacht
mapped to that element.- Since:
- 22.0
-
of
public static <E> ImmutableMultiset<E> of()
Returns the empty immutable multiset.Performance note: the instance returned is a singleton.
-
of
public static <E> ImmutableMultiset<E> of(E element)
Returns an immutable multiset containing a single element.- Throws:
NullPointerException
- ifelement
is null- Since:
- 6.0 (source-compatible since 2.0)
-
of
public static <E> ImmutableMultiset<E> of(E e1, E e2)
Returns an immutable multiset containing the given elements, in order.- Throws:
NullPointerException
- if any element is null- Since:
- 6.0 (source-compatible since 2.0)
-
of
public static <E> ImmutableMultiset<E> of(E e1, E e2, E e3)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException
- if any element is null- Since:
- 6.0 (source-compatible since 2.0)
-
of
public static <E> ImmutableMultiset<E> of(E e1, E e2, E e3, E e4)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException
- if any element is null- Since:
- 6.0 (source-compatible since 2.0)
-
of
public static <E> ImmutableMultiset<E> of(E e1, E e2, E e3, E e4, E e5)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException
- if any element is null- Since:
- 6.0 (source-compatible since 2.0)
-
of
public static <E> ImmutableMultiset<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... others)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException
- if any element is null- Since:
- 6.0 (source-compatible since 2.0)
-
copyOf
public static <E> ImmutableMultiset<E> copyOf(E[] elements)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException
- if any ofelements
is null- Since:
- 6.0
-
copyOf
public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException
- if any ofelements
is null
-
copyOf
public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements)
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException
- if any ofelements
is null
-
iterator
public UnmodifiableIterator<E> iterator()
Description copied from class:ImmutableCollection
Returns an unmodifiable iterator across the elements in this collection.
-
asList
public ImmutableList<E> asList()
Description copied from class:ImmutableCollection
Returns anImmutableList
containing the same elements, in the same order, as this collection.Performance note: in most cases this method can return quickly without actually copying anything. The exact circumstances under which the copy is performed are undefined and subject to change.
- Overrides:
asList
in classImmutableCollection<E>
-
contains
public boolean contains(@CheckForNull Object object)
Description copied from class:java.util.AbstractCollection
Returnstrue
if this collection contains the specified element. More formally, returnstrue
if and only if this collection contains at least one elemente
such thatObjects.equals(o, e)
.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceMultiset<E>
- Specified by:
contains
in classImmutableCollection<E>
- Parameters:
object
- element whose presence in this collection is to be tested- Returns:
true
if this collection contains the specified element
-
add
@CanIgnoreReturnValue @Deprecated public final int add(E element, int occurrences)
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the collection unmodified.- Specified by:
add
in interfaceMultiset<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
- Throws:
UnsupportedOperationException
- always
-
remove
@CanIgnoreReturnValue @Deprecated public final int remove(@CheckForNull Object element, int occurrences)
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the collection unmodified.- Specified by:
remove
in interfaceMultiset<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
- Throws:
UnsupportedOperationException
- always
-
setCount
@CanIgnoreReturnValue @Deprecated public final int setCount(E element, int count)
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the collection unmodified.- Specified by:
setCount
in interfaceMultiset<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
- Throws:
UnsupportedOperationException
- always
-
setCount
@CanIgnoreReturnValue @Deprecated public final boolean setCount(E element, int oldCount, int newCount)
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the collection unmodified.- Specified by:
setCount
in interfaceMultiset<E>
- Parameters:
element
- the element to conditionally set the count of; may be null only if explicitly allowed by the implementationoldCount
- the expected present count of the element in this multisetnewCount
- the desired count of the element in this multiset- Returns:
true
if the condition for modification was met. This implies that the multiset was indeed modified, unlessoldCount == newCount
.- Throws:
UnsupportedOperationException
- always
-
equals
public boolean equals(@CheckForNull Object object)
Description copied from class:java.lang.Object
Indicates whether some other object is "equal to" this one.The
equals
method implements an equivalence relation on non-null object references:- It is reflexive: for any non-null reference value
x
,x.equals(x)
should returntrue
. - It is symmetric: for any non-null reference values
x
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
The
equals
method for classObject
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returnstrue
if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).Note that it is generally necessary to override the
hashCode
method whenever this method is overridden, so as to maintain the general contract for thehashCode
method, which states that equal objects must have equal hash codes.- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceMultiset<E>
- Overrides:
equals
in classObject
- Parameters:
object
- the reference object with which to compare.- Returns:
true
if this object is the same as the obj argument;false
otherwise.- See Also:
Object.hashCode()
,HashMap
- It is reflexive: for any non-null reference value
-
hashCode
public int hashCode()
Description copied from class:java.lang.Object
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided byHashMap
.The general contract of
hashCode
is:- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used inequals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals(Object)
method, then calling thehashCode
method on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal
according to the
Object.equals(java.lang.Object)
method, then calling thehashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class
Object
does return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)- Specified by:
hashCode
in interfaceCollection<E>
- Specified by:
hashCode
in interfaceMultiset<E>
- Overrides:
hashCode
in classObject
- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object)
,System.identityHashCode(java.lang.Object)
- Whenever it is invoked on the same object more than once during
an execution of a Java application, the
-
toString
public String toString()
Description copied from class:java.util.AbstractCollection
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"
). Adjacent elements are separated by the characters", "
(comma and space). Elements are converted to strings as byString.valueOf(Object)
.
-
elementSet
public abstract ImmutableSet<E> elementSet()
Description copied from interface:Multiset
Returns the set of distinct elements contained in this multiset. The element set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. The order of the elements in the element set is unspecified.If the element set supports any removal operations, these necessarily cause all occurrences of the removed element(s) to be removed from the multiset. Implementations are not expected to support the add operations, although this is possible.
A common use for the element set is to find the number of distinct elements in the multiset:
elementSet().size()
.- Specified by:
elementSet
in interfaceMultiset<E>
- Returns:
- a view of the set of distinct elements in this multiset
- Since:
- 21.0 (present with return type
Set
since 2.0)
-
entrySet
public ImmutableSet<Multiset.Entry<E>> entrySet()
Description copied from interface:Multiset
Returns a view of the contents of this multiset, grouped intoMultiset.Entry
instances, each providing an element of the multiset and the count of that element. This set contains exactly one entry for each distinct element in the multiset (thus it always has the same size as theMultiset.elementSet()
). The order of the elements in the element set is unspecified.The entry set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. However, multiset changes may or may not be reflected in any
Entry
instances already retrieved from the entry set (this is implementation-dependent). Furthermore, implementations are not required to support modifications to the entry set at all, and theEntry
instances themselves don't even have methods for modification. See the specific implementation class for more details on how its entry set handles modifications.
-
builder
public static <E> ImmutableMultiset.Builder<E> builder()
Returns a new builder. The generated builder is equivalent to the builder created by theImmutableMultiset.Builder
constructor.
-
-