T
- public final class Multiset<T>
extends java.lang.Object
implements java.lang.Iterable<T>
Set
, but
may have duplicate elements.
Elements of a Multiset that are equal to one another are referred to as
occurrences of the same single element. The total number of
occurrences of an element in a Multiset is called the count of that
element (the terms "frequency" and "multiplicity" are equivalent, but not
used in this API). Since the count of an element is represented as an int
, a Multiset may never contain more than MutableInt#MAX_VALUE
occurrences of any one element.
Constructor and Description |
---|
Multiset() |
Multiset(java.lang.Class<? extends java.util.Map> valueMapType) |
Multiset(java.util.Collection<? extends T> c) |
Multiset(int initialCapacity) |
Multiset(Supplier<? extends java.util.Map<T,?>> mapSupplier) |
Modifier and Type | Method and Description |
---|---|
<E extends java.lang.Exception> |
accept(Try.Consumer<? super Multiset<T>,E> action) |
boolean |
add(T e) |
boolean |
add(T e,
int occurrences) |
boolean |
addAll(java.util.Collection<? extends T> c) |
boolean |
addAll(java.util.Collection<? extends T> c,
int occurrences) |
boolean |
addAll(java.util.Map<? extends T,java.lang.Integer> m) |
boolean |
addAll(Multiset<? extends T> multiset) |
int |
addAndGet(T e) |
int |
addAndGet(T e,
int occurrences) |
boolean |
addIfAbsent(T e) |
boolean |
addIfAbsent(T e,
int occurrences) |
Optional<Pair<java.util.List<T>,java.lang.Integer>> |
allMaxOccurrences() |
Optional<Pair<java.util.List<T>,java.lang.Integer>> |
allMinOccurrences() |
<R,E extends java.lang.Exception> |
apply(Try.Function<? super Multiset<T>,R,E> func) |
OptionalDouble |
averageOfOccurrences() |
void |
clear() |
<E extends java.lang.Exception> |
compute(T key,
Try.BiFunction<? super T,java.lang.Integer,java.lang.Integer,E> remappingFunction)
The implementation is equivalent to performing the following steps for this Multiset:
|
<E extends java.lang.Exception> |
computeIfAbsent(T e,
Try.Function<? super T,java.lang.Integer,E> mappingFunction)
The implementation is equivalent to performing the following steps for this Multiset:
|
<E extends java.lang.Exception> |
computeIfPresent(T e,
Try.BiFunction<? super T,java.lang.Integer,java.lang.Integer,E> remappingFunction)
The implementation is equivalent to performing the following steps for this Multiset:
|
boolean |
contains(java.lang.Object o) |
boolean |
containsAll(java.util.Collection<?> c) |
Multiset<T> |
copy() |
java.util.Set<T> |
elements() |
EntryStream<T,java.lang.Integer> |
entryStream() |
boolean |
equals(java.lang.Object obj) |
<E extends java.lang.Exception> |
filter(Try.BiPredicate<? super T,java.lang.Integer,E> filter) |
<E extends java.lang.Exception> |
filter(Try.Predicate<? super T,E> filter) |
java.util.List<T> |
flatten() |
<E extends java.lang.Exception> |
forEach(Try.ObjIntConsumer<? super T,E> action) |
static <T> Multiset<T> |
from(java.util.Collection<? extends T> coll) |
static <T> Multiset<T> |
from(java.util.Map<? extends T,java.lang.Integer> m) |
int |
get(java.lang.Object e) |
int |
getAndAdd(T e) |
int |
getAndAdd(T e,
int occurrences) |
int |
getAndRemove(java.lang.Object e) |
int |
getAndRemove(java.lang.Object e,
int occurrences) |
int |
getAndSet(T e,
int occurrences)
The element will be removed if the specified count is 0.
|
int |
getOrDefault(java.lang.Object e,
int defaultValue) |
int |
hashCode() |
<R,E extends java.lang.Exception> |
ifNotEmpty(Try.Function<? super Multiset<T>,R,E> func) |
boolean |
isEmpty() |
java.util.Iterator<T> |
iterator() |
Optional<Pair<T,java.lang.Integer>> |
maxOccurrences() |
<E extends java.lang.Exception> |
merge(T key,
int value,
Try.BiFunction<java.lang.Integer,java.lang.Integer,java.lang.Integer,E> remappingFunction)
The implementation is equivalent to performing the following steps for this Multiset:
|
Optional<Pair<T,java.lang.Integer>> |
minOccurrences() |
int |
occurrencesOf(java.lang.Object e) |
static <T> Multiset<T> |
of(T... a) |
boolean |
remove(java.lang.Object e)
Remove one occurrence from the specified elements.
|
boolean |
remove(java.lang.Object e,
int occurrences)
Remove the specified occurrences from the specified element.
|
boolean |
removeAll(java.util.Collection<?> c)
Removes all of this Multiset's elements that are also contained in the
specified collection (optional operation).
|
boolean |
removeAll(java.util.Collection<?> c,
int occurrences)
Remove the specified occurrences from the specified elements.
|
boolean |
removeAll(java.util.Map<?,java.lang.Integer> m) |
boolean |
removeAll(Multiset<?> multiset) |
int |
removeAllOccurrences(java.lang.Object e) |
<E extends java.lang.Exception> |
removeAllOccurrencesIf(Try.BiPredicate<? super T,? super java.lang.Integer,E> predicate) |
<E extends java.lang.Exception> |
removeAllOccurrencesIf(Try.Predicate<? super T,E> predicate) |
int |
removeAndGet(java.lang.Object e) |
int |
removeAndGet(java.lang.Object e,
int occurrences) |
<E extends java.lang.Exception> |
removeIf(int occurrences,
Try.BiPredicate<? super T,? super java.lang.Integer,E> predicate) |
<E extends java.lang.Exception> |
removeIf(int occurrences,
Try.Predicate<? super T,E> predicate) |
<E extends java.lang.Exception> |
replaceAll(Try.BiFunction<? super T,? super java.lang.Integer,java.lang.Integer,E> function)
The associated elements will be removed if zero or negative occurrences are returned by the specified
function . |
<E extends java.lang.Exception> |
replaceIf(Try.BiPredicate<? super T,? super java.lang.Integer,E> predicate,
int newOccurrences) |
<E extends java.lang.Exception> |
replaceIf(Try.Predicate<? super T,E> predicate,
int newOccurrences) |
boolean |
retainAll(java.util.Collection<?> c)
Retains only the elements in this collection that are contained in the
specified collection (optional operation).
|
Multiset<T> |
set(T e,
int occurrences)
The element will be removed if the specified count is 0.
|
Multiset<T> |
setAll(java.util.Collection<? extends T> c,
int occurrences) |
Multiset<T> |
setAll(java.util.Map<? extends T,java.lang.Integer> m) |
Multiset<T> |
setAll(Multiset<? extends T> multiset) |
int |
setAndGet(T e,
int occurrences)
The element will be removed if the specified count is 0.
|
int |
size() |
Stream<java.util.Map.Entry<T,java.lang.Integer>> |
stream() |
long |
sumOfOccurrences() |
java.lang.Object[] |
toArray() |
<A> A[] |
toArray(A[] a) |
ImmutableMap<T,java.lang.Integer> |
toImmutableMap() |
ImmutableMap<T,java.lang.Integer> |
toImmutableMap(IntFunction<? extends java.util.Map<T,java.lang.Integer>> mapSupplier) |
java.util.Map<T,java.lang.Integer> |
toMap() |
<M extends java.util.Map<T,java.lang.Integer>> |
toMap(IntFunction<M> supplier) |
java.util.Map<T,java.lang.Integer> |
toMapSortedByKey(java.util.Comparator<? super T> cmp) |
java.util.Map<T,java.lang.Integer> |
toMapSortedByOccurrences() |
java.util.Map<T,java.lang.Integer> |
toMapSortedByOccurrences(java.util.Comparator<? super java.lang.Integer> cmp) |
java.lang.String |
toString() |
public Multiset()
public Multiset(int initialCapacity)
public Multiset(java.lang.Class<? extends java.util.Map> valueMapType)
public Multiset(java.util.Collection<? extends T> c)
@SafeVarargs public static <T> Multiset<T> of(T... a)
public static <T> Multiset<T> from(java.util.Collection<? extends T> coll)
public static <T> Multiset<T> from(java.util.Map<? extends T,java.lang.Integer> m)
public int get(java.lang.Object e)
e
- public int getOrDefault(java.lang.Object e, int defaultValue)
e
- defaultValue
- public int getAndSet(T e, int occurrences)
e
- occurrences
- public int setAndGet(T e, int occurrences)
e
- occurrences
- public Multiset<T> set(T e, int occurrences)
e
- occurrences
- java.lang.IllegalArgumentException
- if the occurrences of element is less than 0public Multiset<T> setAll(java.util.Map<? extends T,java.lang.Integer> m) throws java.lang.IllegalArgumentException
m
- java.lang.IllegalArgumentException
- if the occurrences of element is less than 0.public Multiset<T> setAll(Multiset<? extends T> multiset) throws java.lang.IllegalArgumentException
m
- java.lang.IllegalArgumentException
- if the occurrences of element is less than 0.public int occurrencesOf(java.lang.Object e)
public long sumOfOccurrences()
java.lang.ArithmeticException
- if total occurrences overflows the maximum value of int.public OptionalDouble averageOfOccurrences()
public boolean add(T e) throws java.lang.IllegalArgumentException
e
- java.lang.IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.public boolean add(T e, int occurrences) throws java.lang.IllegalArgumentException
e
- occurrences
- java.lang.IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.public boolean addIfAbsent(T e) throws java.lang.IllegalArgumentException
e
- java.lang.IllegalArgumentException
public boolean addIfAbsent(T e, int occurrences) throws java.lang.IllegalArgumentException
e
- occurrences
- java.lang.IllegalArgumentException
public int addAndGet(T e)
public int addAndGet(T e, int occurrences)
public int getAndAdd(T e)
public int getAndAdd(T e, int occurrences)
public boolean addAll(java.util.Collection<? extends T> c) throws java.lang.IllegalArgumentException
c
- java.lang.IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.public boolean addAll(java.util.Collection<? extends T> c, int occurrences) throws java.lang.IllegalArgumentException
c
- occurrences
- java.lang.IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.public boolean addAll(java.util.Map<? extends T,java.lang.Integer> m) throws java.lang.IllegalArgumentException
m
- java.lang.IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.public boolean addAll(Multiset<? extends T> multiset) throws java.lang.IllegalArgumentException
m
- java.lang.IllegalArgumentException
- if the occurrences of element is less than 0.public boolean contains(java.lang.Object o)
public boolean containsAll(java.util.Collection<?> c)
public boolean remove(java.lang.Object e)
Multiset
if the occurrences equals to or less than 0 after the operation.e
- occurrences
- public boolean remove(java.lang.Object e, int occurrences)
Multiset
if the occurrences equals to or less than 0 after the operation.e
- occurrences
- public int removeAndGet(java.lang.Object e)
public int removeAndGet(java.lang.Object e, int occurrences)
public int getAndRemove(java.lang.Object e)
public int getAndRemove(java.lang.Object e, int occurrences)
public int removeAllOccurrences(java.lang.Object e)
e
- public <E extends java.lang.Exception> boolean removeAllOccurrencesIf(Try.Predicate<? super T,E> predicate) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> boolean removeAllOccurrencesIf(Try.BiPredicate<? super T,? super java.lang.Integer,E> predicate) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> boolean removeIf(int occurrences, Try.Predicate<? super T,E> predicate) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> boolean removeIf(int occurrences, Try.BiPredicate<? super T,? super java.lang.Integer,E> predicate) throws E extends java.lang.Exception
E extends java.lang.Exception
public boolean removeAll(java.util.Collection<?> c)
c
, and only cares whether or not an element appears at all.c
- Collection.removeAll(Collection)
public boolean removeAll(java.util.Collection<?> c, int occurrences)
c
- occurrences
- the occurrences to remove if the element is in the specified collection c
.public boolean removeAll(java.util.Map<?,java.lang.Integer> m)
m
- public boolean removeAll(Multiset<?> multiset) throws java.lang.IllegalArgumentException
m
- java.lang.IllegalArgumentException
public <E extends java.lang.Exception> boolean replaceIf(Try.Predicate<? super T,E> predicate, int newOccurrences) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> boolean replaceIf(Try.BiPredicate<? super T,? super java.lang.Integer,E> predicate, int newOccurrences) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> void replaceAll(Try.BiFunction<? super T,? super java.lang.Integer,java.lang.Integer,E> function) throws E extends java.lang.Exception
function
.function
- E extends java.lang.Exception
public boolean retainAll(java.util.Collection<?> c)
c
- Collection.retainAll(Collection)
public java.util.Set<T> elements()
public int size()
public boolean isEmpty()
public void clear()
public java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T>
public java.lang.Object[] toArray()
public <A> A[] toArray(A[] a)
public java.util.Map<T,java.lang.Integer> toMap()
public <M extends java.util.Map<T,java.lang.Integer>> M toMap(IntFunction<M> supplier)
public java.util.Map<T,java.lang.Integer> toMapSortedByOccurrences()
public java.util.Map<T,java.lang.Integer> toMapSortedByOccurrences(java.util.Comparator<? super java.lang.Integer> cmp)
public java.util.Map<T,java.lang.Integer> toMapSortedByKey(java.util.Comparator<? super T> cmp)
public ImmutableMap<T,java.lang.Integer> toImmutableMap()
public ImmutableMap<T,java.lang.Integer> toImmutableMap(IntFunction<? extends java.util.Map<T,java.lang.Integer>> mapSupplier)
public java.util.List<T> flatten()
Multiset
public <E extends java.lang.Exception> Multiset<T> filter(Try.Predicate<? super T,E> filter) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> Multiset<T> filter(Try.BiPredicate<? super T,java.lang.Integer,E> filter) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> void forEach(Try.ObjIntConsumer<? super T,E> action) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> int computeIfAbsent(T e, Try.Function<? super T,java.lang.Integer,E> mappingFunction) throws E extends java.lang.Exception
final int oldValue = get(e); if (oldValue > 0) { return oldValue; } final int newValue = mappingFunction.apply(e); if (newValue > 0) { set(e, newValue); } return newValue;
e
- mappingFunction
- E extends java.lang.Exception
public <E extends java.lang.Exception> int computeIfPresent(T e, Try.BiFunction<? super T,java.lang.Integer,java.lang.Integer,E> remappingFunction) throws E extends java.lang.Exception
final int oldValue = get(e); if (oldValue == 0) { return oldValue; } final int newValue = remappingFunction.apply(e, oldValue); if (newValue > 0) { set(e, newValue); } else { remove(e); } return newValue;
e
- remappingFunction
- E extends java.lang.Exception
public <E extends java.lang.Exception> int compute(T key, Try.BiFunction<? super T,java.lang.Integer,java.lang.Integer,E> remappingFunction) throws E extends java.lang.Exception
final int oldValue = get(key); final int newValue = remappingFunction.apply(key, oldValue); if (newValue > 0) { set(key, newValue); } else { if (oldValue > 0) { remove(key); } } return newValue;
key
- remappingFunction
- E extends java.lang.Exception
public <E extends java.lang.Exception> int merge(T key, int value, Try.BiFunction<java.lang.Integer,java.lang.Integer,java.lang.Integer,E> remappingFunction) throws E extends java.lang.Exception
int oldValue = get(key); int newValue = (oldValue == 0) ? value : remappingFunction.apply(oldValue, value); if (newValue > 0) { set(key, newValue); } else { if (oldValue > 0) { remove(key); } } return newValue;
key
- value
- remappingFunction
- E extends java.lang.Exception
public EntryStream<T,java.lang.Integer> entryStream()
public <R,E extends java.lang.Exception> Optional<R> ifNotEmpty(Try.Function<? super Multiset<T>,R,E> func) throws E extends java.lang.Exception
E extends java.lang.Exception
public <R,E extends java.lang.Exception> R apply(Try.Function<? super Multiset<T>,R,E> func) throws E extends java.lang.Exception
E extends java.lang.Exception
public <E extends java.lang.Exception> void accept(Try.Consumer<? super Multiset<T>,E> action) throws E extends java.lang.Exception
E extends java.lang.Exception
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object