Package com.landawn.abacus.util
Class Multiset<T>
java.lang.Object
com.landawn.abacus.util.Multiset<T>
- Type Parameters:
T
-
- All Implemented Interfaces:
Iterable<T>
A collection that supports order-independent equality, like
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 Integer.MAX_VALUE
occurrences of any one element.
- Since:
- 0.8
- Author:
- Haiyang Li
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<E extends Exception>
voidaccept
(Throwables.Consumer<? super Multiset<T>, E> action) acceptIfNotEmpty
(Throwables.Consumer<? super Multiset<T>, E> action) Accept if not empty.boolean
boolean
boolean
Adds the all.boolean
addAll
(Collection<? extends T> c) Adds the all.boolean
addAll
(Collection<? extends T> c, int occurrencesToAdd) Adds the all.boolean
Adds the all.int
Adds the and get.int
Adds the and get.boolean
addIfAbsent
(T e) Adds the if absent.boolean
addIfAbsent
(T e, int occurrencesToAdd) Adds the if absent.u.Optional<Pair<Integer,
List<T>>> All max occurrences.u.Optional<Pair<Integer,
List<T>>> All min occurrences.<R,
E extends Exception>
Rapply
(Throwables.Function<? super Multiset<T>, ? extends R, E> func) <R,
E extends Exception>
u.Optional<R>applyIfNotEmpty
(Throwables.Function<? super Multiset<T>, ? extends R, E> func) Apply if not empty.Average of occurrences.void
clear()
Clear.<E extends Exception>
intThe implementation is equivalent to performing the following steps for this Multiset:<E extends Exception>
intcomputeIfAbsent
(T e, Throwables.Function<? super T, Integer, E> mappingFunction) The implementation is equivalent to performing the following steps for this Multiset:<E extends Exception>
intcomputeIfPresent
(T e, Throwables.BiFunction<? super T, Integer, Integer, E> remappingFunction) The implementation is equivalent to performing the following steps for this Multiset:boolean
boolean
containsAll
(Collection<?> c) copy()
static <T> Multiset<T>
create
(Collection<? extends T> coll) static <T> Multiset<T>
static <T,
V> Multiset<T> create
(Map<? extends T, ? extends V> m, ToIntFunction<? super V> valueMapper) static <T> Multiset<T>
elements()
boolean
filter
(Throwables.BiPredicate<? super T, Integer, E> filter) filter
(Throwables.Predicate<? super T, E> filter) flatten()
<E extends Exception>
voidforEach
(Throwables.Consumer<? super T, E> action) <E extends Exception>
voidforEach
(Throwables.ObjIntConsumer<? super T, E> action) int
int
Gets the and add.int
Gets the and add.int
Gets the and remove.int
getAndRemove
(Object e, int occurrencesToRemove) Gets the and remove.int
The element will be removed if the specified count is 0.int
getOrDefault
(Object e, int defaultValue) Gets the or default.int
hashCode()
boolean
isEmpty()
Checks if is empty.iterator()
<E extends Exception>
intThe implementation is equivalent to performing the following steps for this Multiset:int
static <T> Multiset<T>
of
(T... a) boolean
Remove one occurrence from the specified elements.boolean
Remove the specified occurrences from the specified element.boolean
Removes the all.boolean
removeAll
(Collection<?> c) Remove one occurrence for each element in the specified Collection.boolean
removeAll
(Collection<?> c, int occurrencesToRemove) Remove the specified occurrences for each element in the specified Collection.boolean
Removes the all.int
Removes the all occurrences.boolean
Removes the all occurrences.<E extends Exception>
booleanremoveAllOccurrencesIf
(Throwables.BiPredicate<? super T, ? super Integer, E> predicate) Removes the all occurrences if.<E extends Exception>
booleanremoveAllOccurrencesIf
(Throwables.Predicate<? super T, E> predicate) Removes the all occurrences if.int
Removes the and get.int
removeAndGet
(Object e, int occurrencesToRemove) Removes the and get.<E extends Exception>
booleanremoveIf
(int occurrencesToRemove, Throwables.BiPredicate<? super T, ? super Integer, E> predicate) Removes the if.<E extends Exception>
booleanremoveIf
(int occurrencesToRemove, Throwables.Predicate<? super T, E> predicate) Removes the if.<E extends Exception>
voidreplaceAll
(Throwables.BiFunction<? super T, ? super Integer, Integer, E> function) The associated elements will be removed if zero or negative occurrences are returned by the specifiedfunction
.<E extends Exception>
booleanreplaceIf
(Throwables.BiPredicate<? super T, ? super Integer, E> predicate, int newOccurrences) <E extends Exception>
booleanreplaceIf
(Throwables.Predicate<? super T, E> predicate, int newOccurrences) boolean
retainAll
(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation).The element will be removed if the specified count is 0.Sets the all.setAll
(Collection<? extends T> c, int occurrences) Sets the all.Sets the all.int
The element will be removed if the specified count is 0.int
size()
stream()
long
Sum of occurrences.Object[]
toArray()
<A> A[]
toArray
(A[] a) To immutable map.toImmutableMap
(IntFunction<? extends Map<T, Integer>> mapSupplier) To immutable map.toMap()
toMap
(IntFunction<? extends M> supplier) toMapSortedByKey
(Comparator<? super T> cmp) To map sorted by key.To map sorted by occurrences.toMapSortedByOccurrences
(Comparator<? super Integer> cmp) To map sorted by occurrences.toString()
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Multiset
public Multiset() -
Multiset
public Multiset(int initialCapacity) - Parameters:
initialCapacity
-
-
Multiset
- Parameters:
c
-
-
Multiset
- Parameters:
valueMapType
-
-
Multiset
- Parameters:
mapSupplier
-
-
-
Method Details
-
of
- Type Parameters:
T
-- Parameters:
a
-- Returns:
-
create
- Type Parameters:
T
-- Parameters:
coll
-- Returns:
-
create
- Type Parameters:
T
-- Parameters:
iter
-- Returns:
-
create
- Type Parameters:
T
-- Parameters:
m
-- Returns:
-
create
@Beta public static <T,V> Multiset<T> create(Map<? extends T, ? extends V> m, ToIntFunction<? super V> valueMapper) - Type Parameters:
T
-V
-- Parameters:
m
-valueMapper
-- Returns:
-
get
- Parameters:
e
-- Returns:
-
getOrDefault
Gets the or default.- Parameters:
e
-defaultValue
-- Returns:
-
getAndSet
The element will be removed if the specified count is 0.- Parameters:
e
-occurrences
-- Returns:
-
setAndGet
The element will be removed if the specified count is 0.- Parameters:
e
-occurrences
-- Returns:
-
set
The element will be removed if the specified count is 0.- Parameters:
e
-occurrences
-- Returns:
- this Multiset.
- Throws:
IllegalArgumentException
- if the occurrences of element is less than 0
-
setAll
Sets the all.- Parameters:
c
-occurrences
-- Returns:
-
setAll
Sets the all.- Parameters:
m
-- Returns:
- this Multiset.
- Throws:
IllegalArgumentException
- if the occurrences of element is less than 0.
-
setAll
Sets the all.- Parameters:
multiset
-- Returns:
- this Multiset.
- Throws:
IllegalArgumentException
- if the occurrences of element is less than 0.
-
occurrencesOf
- Parameters:
e
-- Returns:
-
minOccurrences
- Returns:
-
maxOccurrences
- Returns:
-
allMinOccurrences
All min occurrences.- Returns:
-
allMaxOccurrences
All max occurrences.- Returns:
-
sumOfOccurrences
public long sumOfOccurrences()Sum of occurrences.- Returns:
- Throws:
ArithmeticException
- if total occurrences overflows the maximum value of int.
-
averageOfOccurrences
Average of occurrences.- Returns:
-
add
- Parameters:
e
-- Returns:
- always true
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.
-
add
- Parameters:
e
-occurrencesToAdd
-- Returns:
- true if the specified occurrences is bigger than 0.
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.
-
addIfAbsent
Adds the if absent.- Parameters:
e
-- Returns:
- true if the specified element is absent.
- Throws:
IllegalArgumentException
- the illegal argument exception
-
addIfAbsent
Adds the if absent.- Parameters:
e
-occurrencesToAdd
-- Returns:
- true if the specified element is absent and occurrences is bigger than 0.
- Throws:
IllegalArgumentException
- the illegal argument exception
-
addAndGet
Adds the and get.- Parameters:
e
-- Returns:
-
addAndGet
Adds the and get.- Parameters:
e
-occurrencesToAdd
-- Returns:
-
getAndAdd
Gets the and add.- Parameters:
e
-- Returns:
-
getAndAdd
Gets the and add.- Parameters:
e
-occurrencesToAdd
-- Returns:
-
addAll
Adds the all.- Parameters:
c
-- Returns:
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.
-
addAll
public boolean addAll(Collection<? extends T> c, int occurrencesToAdd) throws IllegalArgumentException Adds the all.- Parameters:
c
-occurrencesToAdd
-- Returns:
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.
-
addAll
Adds the all.- Parameters:
m
-- Returns:
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Integer.MAX_VALUE.
-
addAll
Adds the all.- Parameters:
multiset
-- Returns:
- Throws:
IllegalArgumentException
- if the occurrences of element is less than 0.
-
contains
- Parameters:
o
-- Returns:
-
containsAll
- Parameters:
c
-- Returns:
-
remove
Remove one occurrence from the specified elements. The element will be removed from thisMultiset
if the occurrences equals to or less than 0 after the operation.- Parameters:
e
-- Returns:
-
remove
Remove the specified occurrences from the specified element. The element will be removed from thisMultiset
if the occurrences equals to or less than 0 after the operation.- Parameters:
e
-occurrencesToRemove
-- Returns:
-
removeAndGet
Removes the and get.- Parameters:
e
-- Returns:
-
removeAndGet
Removes the and get.- Parameters:
e
-occurrencesToRemove
-- Returns:
-
getAndRemove
Gets the and remove.- Parameters:
e
-- Returns:
-
getAndRemove
Gets the and remove.- Parameters:
e
-occurrencesToRemove
-- Returns:
-
removeAllOccurrences
Removes the all occurrences.- Parameters:
e
-- Returns:
-
removeAllOccurrencesIf
public <E extends Exception> boolean removeAllOccurrencesIf(Throwables.Predicate<? super T, E> predicate) throws ERemoves the all occurrences if.- Type Parameters:
E
-- Parameters:
predicate
-- Returns:
- Throws:
E
- the e
-
removeAllOccurrencesIf
public <E extends Exception> boolean removeAllOccurrencesIf(Throwables.BiPredicate<? super T, ? super Integer, throws EE> predicate) Removes the all occurrences if.- Type Parameters:
E
-- Parameters:
predicate
-- Returns:
- Throws:
E
- the e
-
removeAllOccurrencesForEach
Removes the all occurrences.- Parameters:
c
-- Returns:
-
removeIf
public <E extends Exception> boolean removeIf(int occurrencesToRemove, Throwables.Predicate<? super T, E> predicate) throws ERemoves the if.- Type Parameters:
E
-- Parameters:
occurrencesToRemove
-predicate
-- Returns:
- Throws:
E
- the e
-
removeIf
public <E extends Exception> boolean removeIf(int occurrencesToRemove, Throwables.BiPredicate<? super T, ? super Integer, throws EE> predicate) Removes the if.- Type Parameters:
E
-- Parameters:
occurrencesToRemove
-predicate
-- Returns:
- Throws:
E
- the e
-
removeAll
Remove one occurrence for each element in the specified Collection. The elements will be removed from this set if the occurrences equals to or less than 0 after the operation.- Parameters:
c
-- Returns:
- true if this set changed as a result of the call
-
removeAll
Remove the specified occurrences for each element in the specified Collection. The elements will be removed from this set if the occurrences equals to or less than 0 after the operation.- Parameters:
c
-occurrencesToRemove
- the occurrences to remove if the element is in the specified collectionc
.- Returns:
- true if this set changed as a result of the call
-
removeAll
Removes the all.- Parameters:
m
-- Returns:
-
removeAll
Removes the all.- Parameters:
multiset
-- Returns:
- Throws:
IllegalArgumentException
- the illegal argument exception
-
replaceIf
public <E extends Exception> boolean replaceIf(Throwables.Predicate<? super T, E> predicate, int newOccurrences) throws E- Type Parameters:
E
-- Parameters:
predicate
-newOccurrences
-- Returns:
- Throws:
E
- the e
-
replaceIf
public <E extends Exception> boolean replaceIf(Throwables.BiPredicate<? super T, ? super Integer, throws EE> predicate, int newOccurrences) - Type Parameters:
E
-- Parameters:
predicate
-newOccurrences
-- Returns:
- Throws:
E
- the e
-
replaceAll
public <E extends Exception> void replaceAll(Throwables.BiFunction<? super T, ? super Integer, throws EInteger, E> function) The associated elements will be removed if zero or negative occurrences are returned by the specifiedfunction
.- Type Parameters:
E
-- Parameters:
function
-- Throws:
E
- the e
-
retainAll
Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection.- Parameters:
c
-- Returns:
- true if this set changed as a result of the call
- See Also:
-
copy
- Returns:
-
elements
- Returns:
-
size
public int size()- Returns:
-
isEmpty
public boolean isEmpty()Checks if is empty.- Returns:
- true, if is empty
-
clear
public void clear()Clear. -
iterator
-
flatIterator
- Returns:
-
toArray
- Returns:
-
toArray
public <A> A[] toArray(A[] a) - Type Parameters:
A
-- Parameters:
a
-- Returns:
-
toMap
- Returns:
-
toMap
- Type Parameters:
M
-- Parameters:
supplier
-- Returns:
-
toMapSortedByOccurrences
To map sorted by occurrences.- Returns:
-
toMapSortedByOccurrences
To map sorted by occurrences.- Parameters:
cmp
-- Returns:
-
toMapSortedByKey
To map sorted by key.- Parameters:
cmp
-- Returns:
-
toImmutableMap
To immutable map.- Returns:
-
toImmutableMap
To immutable map.- Parameters:
mapSupplier
-- Returns:
-
flatten
- Returns:
- a list with all elements, each of them is repeated with the occurrences in this
Multiset
-
filter
- Type Parameters:
E
-- Parameters:
filter
-- Returns:
- Throws:
E
- the e
-
filter
public <E extends Exception> Multiset<T> filter(Throwables.BiPredicate<? super T, Integer, throws EE> filter) - Type Parameters:
E
-- Parameters:
filter
-- Returns:
- Throws:
E
- the e
-
forEach
- Type Parameters:
E
-- Parameters:
action
-- Throws:
E
- the e
-
forEach
- Type Parameters:
E
-- Parameters:
action
-- Throws:
E
- the e
-
computeIfAbsent
public <E extends Exception> int computeIfAbsent(T e, Throwables.Function<? super T, Integer, throws EE> mappingFunction) The implementation is equivalent to performing the following steps for this Multiset:final int oldValue = get(e); if (oldValue > 0) { return oldValue; } final int newValue = mappingFunction.apply(e); if (newValue > 0) { set(e, newValue); } return newValue;
- Type Parameters:
E
-- Parameters:
e
-mappingFunction
-- Returns:
- Throws:
E
- the e
-
computeIfPresent
public <E extends Exception> int computeIfPresent(T e, Throwables.BiFunction<? super T, Integer, throws EInteger, E> remappingFunction) The implementation is equivalent to performing the following steps for this Multiset: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;
- Type Parameters:
E
-- Parameters:
e
-remappingFunction
-- Returns:
- Throws:
E
- the e
-
compute
public <E extends Exception> int compute(T key, Throwables.BiFunction<? super T, Integer, throws EInteger, E> remappingFunction) The implementation is equivalent to performing the following steps for this Multiset: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;
- Type Parameters:
E
-- Parameters:
key
-remappingFunction
-- Returns:
- Throws:
E
- the e
-
merge
public <E extends Exception> int merge(T key, int value, Throwables.BiFunction<Integer, Integer, throws EInteger, E> remappingFunction) The implementation is equivalent to performing the following steps for this Multiset: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;
- Type Parameters:
E
-- Parameters:
key
-value
-remappingFunction
-- Returns:
- Throws:
E
- the e
-
stream
- Returns:
-
flatStream
- Returns:
-
entryStream
- Returns:
-
apply
public <R,E extends Exception> R apply(Throwables.Function<? super Multiset<T>, ? extends R, throws EE> func) - Type Parameters:
R
-E
-- Parameters:
func
-- Returns:
- Throws:
E
- the e
-
applyIfNotEmpty
public <R,E extends Exception> u.Optional<R> applyIfNotEmpty(Throwables.Function<? super Multiset<T>, ? extends R, throws EE> func) Apply if not empty.- Type Parameters:
R
-E
-- Parameters:
func
-- Returns:
- Throws:
E
- the e
-
accept
public <E extends Exception> void accept(Throwables.Consumer<? super Multiset<T>, E> action) throws E- Type Parameters:
E
-- Parameters:
action
-- Throws:
E
- the e
-
acceptIfNotEmpty
public <E extends Exception> If.OrElse acceptIfNotEmpty(Throwables.Consumer<? super Multiset<T>, E> action) throws EAccept if not empty.- Type Parameters:
E
-- Parameters:
action
-- Returns:
- Throws:
E
- the e
-
hashCode
public int hashCode() -
equals
-
toString
-