@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableMultiset<E> extends ImmutableCollection<E> implements Multiset<E>
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.
Modifier and Type | Class and Description |
---|---|
static class |
ImmutableMultiset.Builder<E>
A builder for creating immutable multiset instances, especially
public static final
multisets ("constant multisets"). |
Multiset.Entry<E>
Modifier and Type | Method and Description |
---|---|
int |
add(E element,
int occurrences)
Deprecated.
Unsupported operation.
|
ImmutableList<E> |
asList()
Returns an
ImmutableList 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)
Returns true 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 into
Multiset.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 a
Collector that accumulates the input elements into a new ImmutableMultiset . |
static <T,E> Collector<T,?,ImmutableMultiset<E>> |
toImmutableMultiset(Function<? super T,? extends E> elementFunction,
ToIntFunction<? super T> countFunction)
Returns a
Collector that accumulates elements into an ImmutableMultiset whose
elements are the result of applying elementFunction to the inputs, with counts equal to
the result of applying countFunction to the inputs. |
String |
toString()
Returns a string representation of this collection.
|
add, addAll, clear, remove, removeAll, removeIf, retainAll, spliterator, toArray, toArray
containsAll, isEmpty, size
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
add, containsAll, count, forEach, forEachEntry, remove, removeAll, retainAll, size, spliterator
addAll, clear, isEmpty, parallelStream, removeIf, stream, toArray, toArray
@Beta public static <E> Collector<E,?,ImmutableMultiset<E>> toImmutableMultiset()
Collector
that accumulates the input elements into a new ImmutableMultiset
. Elements iterate in order by the first appearance of that element in
encounter order.public static <T,E> Collector<T,?,ImmutableMultiset<E>> toImmutableMultiset(Function<? super T,? extends E> elementFunction, ToIntFunction<? super T> countFunction)
Collector
that accumulates elements into an ImmutableMultiset
whose
elements are the result of applying elementFunction
to the inputs, with counts equal to
the result of applying countFunction
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 of countFunction.applyAsInt(t)
for each t
mapped to that element.
public static <E> ImmutableMultiset<E> of()
public static <E> ImmutableMultiset<E> of(E element)
NullPointerException
- if element
is nullpublic static <E> ImmutableMultiset<E> of(E e1, E e2)
NullPointerException
- if any element is nullpublic static <E> ImmutableMultiset<E> of(E e1, E e2, E e3)
NullPointerException
- if any element is nullpublic static <E> ImmutableMultiset<E> of(E e1, E e2, E e3, E e4)
NullPointerException
- if any element is nullpublic static <E> ImmutableMultiset<E> of(E e1, E e2, E e3, E e4, E e5)
NullPointerException
- if any element is nullpublic static <E> ImmutableMultiset<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... others)
NullPointerException
- if any element is nullpublic static <E> ImmutableMultiset<E> copyOf(E[] elements)
NullPointerException
- if any of elements
is nullpublic static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements)
NullPointerException
- if any of elements
is nullpublic static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements)
NullPointerException
- if any of elements
is nullpublic UnmodifiableIterator<E> iterator()
ImmutableCollection
public ImmutableList<E> asList()
ImmutableCollection
ImmutableList
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.
asList
in class ImmutableCollection<E>
public boolean contains(@NullableDecl Object object)
java.util.AbstractCollection
This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element.
contains
in interface Multiset<E>
contains
in interface Collection<E>
contains
in class ImmutableCollection<E>
object
- element whose presence in this collection is to be tested@CanIgnoreReturnValue @Deprecated public final int add(E element, int occurrences)
add
in interface Multiset<E>
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.UnsupportedOperationException
- always@CanIgnoreReturnValue @Deprecated public final int remove(Object element, int occurrences)
remove
in interface Multiset<E>
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.UnsupportedOperationException
- always@CanIgnoreReturnValue @Deprecated public final int setCount(E element, int count)
setCount
in interface Multiset<E>
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 multisetUnsupportedOperationException
- always@CanIgnoreReturnValue @Deprecated public final boolean setCount(E element, int oldCount, int newCount)
setCount
in interface Multiset<E>
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 multisettrue
if the condition for modification was met. This implies that the multiset
was indeed modified, unless oldCount == newCount
.UnsupportedOperationException
- alwayspublic boolean equals(@NullableDecl Object object)
java.lang.Object
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y)
consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
equals
in interface Multiset<E>
equals
in interface Collection<E>
equals
in class Object
object
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
java.lang.Object
HashMap
.
The general contract of hashCode
is:
hashCode
method
must consistently return the same integer, provided no information
used in equals
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.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode
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. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
Java™ programming language.)
hashCode
in interface Multiset<E>
hashCode
in interface Collection<E>
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
java.util.AbstractCollection
String.valueOf(Object)
.public abstract ImmutableSet<E> elementSet()
Multiset
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()
.
elementSet
in interface Multiset<E>
Set
since 2.0)public ImmutableSet<Multiset.Entry<E>> entrySet()
Multiset
Multiset.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 the Multiset.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 the Entry
instances themselves don't even
have methods for modification. See the specific implementation class for more details on how
its entry set handles modifications.
public static <E> ImmutableMultiset.Builder<E> builder()
ImmutableMultiset.Builder
constructor.Copyright © 2010–2017. All rights reserved.