Class ImmutableMultiset<E>
- All Implemented Interfaces:
Multiset<E>, Serializable, Iterable<E>, Collection<E>
- Direct Known Subclasses:
ImmutableSortedMultiset
Multiset whose contents will never change, with many other important properties
detailed at ImmutableCollection.
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder for creating immutable multiset instances, especiallypublic static finalmultisets ("constant multisets").Nested classes/interfaces inherited from interface Multiset
Multiset.Entry<E> -
Method Summary
Modifier and TypeMethodDescriptionfinal intDeprecated.Unsupported operation.asList()Returns anImmutableListcontaining the same elements, in the same order, as this collection.static <E> ImmutableMultiset.Builder<E> builder()Returns a new builder.booleanDetermines whether this multiset 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> Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.static <E> ImmutableMultiset<E> Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.abstract ImmutableSet<E> Returns the set of distinct elements contained in this multiset.entrySet()Returns a view of the contents of this multiset, grouped intoMultiset.Entryinstances, each providing an element of the multiset and the count of that element.booleanCompares the specified object with this multiset for equality.inthashCode()Returns the hash code for this multiset.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 e1) 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.final intDeprecated.Unsupported operation.final intDeprecated.Unsupported operation.final booleanDeprecated.Unsupported operation.static <E> Collector<E, ?, ImmutableMultiset<E>> Returns aCollectorthat 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 aCollectorthat accumulates elements into anImmutableMultisetwhose elements are the result of applyingelementFunctionto the inputs, with counts equal to the result of applyingcountFunctionto the inputs.toString()Methods inherited from class ImmutableCollection
add, addAll, clear, remove, removeAll, removeIf, retainAll, spliterator, toArray, toArrayMethods inherited from class AbstractCollection
containsAll, isEmpty, sizeMethods inherited from interface Collection
addAll, clear, isEmpty, parallelStream, removeIf, stream, toArray, toArray, toArrayMethods inherited from interface Multiset
add, containsAll, count, forEach, forEachEntry, remove, removeAll, retainAll, size, spliterator
-
Method Details
-
toImmutableMultiset
Returns aCollectorthat 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 aCollectorthat accumulates elements into anImmutableMultisetwhose elements are the result of applyingelementFunctionto the inputs, with counts equal to the result of applyingcountFunctionto the inputs.If the mapped elements contain duplicates (according to
Object.equals(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 eachtmapped to that element.- Since:
- 22.0
-
of
Returns the empty immutable multiset.Performance note: the instance returned is a singleton.
-
of
Returns an immutable multiset containing a single element.- Throws:
NullPointerException- if the element is null- Since:
- 6.0 (source-compatible since 2.0)
-
of
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
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
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
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
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
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException- if any ofelementsis null- Since:
- 6.0
-
copyOf
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException- if any ofelementsis null
-
copyOf
Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.- Throws:
NullPointerException- if any ofelementsis null
-
iterator
Description copied from class:ImmutableCollectionReturns an unmodifiable iterator across the elements in this collection. -
asList
Description copied from class:ImmutableCollectionReturns anImmutableListcontaining 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:
asListin classImmutableCollection<E>
-
contains
Description copied from interface:MultisetDetermines whether this multiset contains the specified element.This method refines
Collection.contains(Object)to further specify that it may not throw an exception in response toelementbeing null or of the wrong type.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceMultiset<E>- Specified by:
containsin classImmutableCollection<E>- Parameters:
object- the element to check for- Returns:
trueif this multiset contains at least one occurrence of the element
-
add
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the collection unmodified.- Specified by:
addin 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(@Nullable Object element, int occurrences) Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the collection unmodified.- Specified by:
removein 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
Deprecated.Unsupported operation.Guaranteed to throw an exception and leave the collection unmodified.- Specified by:
setCountin 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:
setCountin 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:
trueif the condition for modification was met. This implies that the multiset was indeed modified, unlessoldCount == newCount.- Throws:
UnsupportedOperationException- always
-
equals
Description copied from interface:MultisetCompares the specified object with this multiset for equality. Returnstrueif the given object is also a multiset and contains equal elements with equal counts, regardless of order. -
hashCode
public int hashCode()Description copied from interface:MultisetReturns the hash code for this multiset. This is defined as the sum of((element == null) ? 0 : element.hashCode()) ^ count(element)over all distinct elements in the multiset. It follows that a multiset and its entry set always have the same hash code.
-
toString
Description copied from interface:MultisetIt is recommended, though not mandatory, that this method return the result of invoking
Multiset.toString()on theMultiset.entrySet(), yielding a result such as[a x 3, c, d x 2, e]. -
elementSet
Description copied from interface:MultisetReturns 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:
elementSetin interfaceMultiset<E>- Returns:
- a view of the set of distinct elements in this multiset
- Since:
- 21.0 (present with return type
Setsince 2.0)
-
entrySet
Description copied from interface:MultisetReturns a view of the contents of this multiset, grouped intoMultiset.Entryinstances, 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
Entryinstances 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 theEntryinstances themselves don't even have methods for modification. See the specific implementation class for more details on how its entry set handles modifications. -
builder
Returns a new builder. The generated builder is equivalent to the builder created by theImmutableMultiset.Builderconstructor.
-