Package com.landawn.abacus.util
Class LongMultiset<T>
java.lang.Object
com.landawn.abacus.util.LongMultiset<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 LongMultiset 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 LongMultiset 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
long
, a LongMultiset may never contain more than MutableLong#MAX_VALUE
occurrences of any one element.
- Since:
- 0.8
- Author:
- Haiyang Li
-
Constructor Summary
ConstructorDescriptionLongMultiset
(int initialCapacity) LongMultiset
(Class<? extends Map> valueMapType) LongMultiset
(Collection<? extends T> c) LongMultiset
(Supplier<? extends Map<? extends T, ?>> mapSupplier) -
Method Summary
Modifier and TypeMethodDescription<E extends Exception>
voidaccept
(Throwables.Consumer<? super LongMultiset<T>, E> action) acceptIfNotEmpty
(Throwables.Consumer<? super LongMultiset<T>, E> action) Accept if not empty.boolean
boolean
boolean
addAll
(LongMultiset<? extends T> multiset) Adds the all.boolean
addAll
(Collection<? extends T> c) Adds the all.boolean
addAll
(Collection<? extends T> c, long occurrencesToAdd) Adds the all.boolean
Adds the all.long
Adds the and get.long
Adds the and get.boolean
addIfAbsent
(T e) Adds the if absent.boolean
addIfAbsent
(T e, long occurrencesToAdd) Adds the if absent.u.Optional<Pair<Long,
List<T>>> All max occurrences.u.Optional<Pair<Long,
List<T>>> All min occurrences.<R,
E extends Exception>
Rapply
(Throwables.Function<? super LongMultiset<T>, ? extends R, E> func) <R,
E extends Exception>
u.Optional<R>applyIfNotEmpty
(Throwables.Function<? super LongMultiset<T>, ? extends R, E> func) Apply if not empty.Average of occurrences.void
clear()
Clear.<E extends Exception>
longThe implementation is equivalent to performing the following steps for this LongMultiset:<E extends Exception>
longcomputeIfAbsent
(T e, Throwables.Function<? super T, Long, E> mappingFunction) The implementation is equivalent to performing the following steps for this LongMultiset:<E extends Exception>
longcomputeIfPresent
(T e, Throwables.BiFunction<? super T, Long, Long, E> remappingFunction) The implementation is equivalent to performing the following steps for this LongMultiset:boolean
boolean
containsAll
(Collection<?> c) copy()
static <T> LongMultiset<T>
static <T> LongMultiset<T>
create
(Collection<? extends T> coll) static <T> LongMultiset<T>
static <T,
V> LongMultiset<T> create
(Map<? extends T, ? extends V> m, ToLongFunction<? super V> valueMapper) static <T> LongMultiset<T>
elements()
boolean
<E extends Exception>
LongMultiset<T>filter
(Throwables.BiPredicate<? super T, Long, E> filter) <E extends Exception>
LongMultiset<T>filter
(Throwables.Predicate<? super T, E> filter) flatten()
<E extends Exception>
voidforEach
(Throwables.Consumer<? super T, E> action) <E extends Exception>
voidforEach
(Throwables.ObjLongConsumer<? super T, E> action) long
long
Gets the and add.long
Gets the and add.long
Gets the and remove.long
getAndRemove
(Object e, long occurrencesToRemove) Gets the and remove.long
The element will be removed if the specified count is 0.long
getOrDefault
(Object e, long defaultValue) Gets the or default.int
hashCode()
boolean
isEmpty()
Checks if is empty.iterator()
u.Optional<Pair<Long,
T>> <E extends Exception>
longThe implementation is equivalent to performing the following steps for this LongMultiset:u.Optional<Pair<Long,
T>> long
static <T> LongMultiset<T>
of
(T... a) boolean
Remove one occurrence from the specified elements.boolean
Remove the specified occurrences from the specified element.boolean
removeAll
(LongMultiset<?> multiset) Removes the all.boolean
removeAll
(Collection<?> c) Remove one occurrence for each element in the specified Collection.boolean
removeAll
(Collection<?> c, long occurrencesToRemove) Remove the specified occurrences for each element in the specified Collection.boolean
Removes the all.long
Removes the all occurrences.boolean
Removes the all occurrences.<E extends Exception>
booleanremoveAllOccurrencesIf
(Throwables.BiPredicate<? super T, ? super Long, E> predicate) Removes the all occurrences if.<E extends Exception>
booleanremoveAllOccurrencesIf
(Throwables.Predicate<? super T, E> predicate) Removes the all occurrences if.long
Removes the and get.long
removeAndGet
(Object e, long occurrencesToRemove) Removes the and get.<E extends Exception>
booleanremoveIf
(long occurrences, Throwables.BiPredicate<? super T, ? super Long, E> predicate) Removes the if.<E extends Exception>
booleanremoveIf
(long occurrences, Throwables.Predicate<? super T, E> predicate) Removes the if.<E extends Exception>
voidreplaceAll
(Throwables.BiFunction<? super T, ? super Long, Long, 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 Long, E> predicate, long newOccurrences) <E extends Exception>
booleanreplaceIf
(Throwables.Predicate<? super T, E> predicate, long 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.setAll
(LongMultiset<? extends T> multiset) Sets the all.setAll
(Collection<? extends T> c, long occurrences) Sets the all.Sets the all.long
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, Long>> 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 Long> cmp) To map sorted by occurrences.toString()
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
LongMultiset
public LongMultiset() -
LongMultiset
public LongMultiset(int initialCapacity) - Parameters:
initialCapacity
-
-
LongMultiset
- Parameters:
c
-
-
LongMultiset
- Parameters:
valueMapType
-
-
LongMultiset
- 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> LongMultiset<T> create(Map<? extends T, ? extends V> m, ToLongFunction<? super V> valueMapper) - Type Parameters:
T
-V
-- Parameters:
m
-valueMapper
-- Returns:
-
create
- Type Parameters:
T
-- Parameters:
multiset
-- 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 LongMultiset.
- 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 LongMultiset.
- Throws:
IllegalArgumentException
- if the occurrences of element is less than 0.
-
setAll
Sets the all.- Parameters:
multiset
-- Returns:
- this LongMultiset.
- 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 long.
-
averageOfOccurrences
Average of occurrences.- Returns:
-
add
- Parameters:
e
-- Returns:
- always true
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Long.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 Long.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 Long.MAX_VALUE.
-
addAll
public boolean addAll(Collection<? extends T> c, long occurrencesToAdd) throws IllegalArgumentException Adds the all.- Parameters:
c
-occurrencesToAdd
-- Returns:
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Long.MAX_VALUE.
-
addAll
Adds the all.- Parameters:
m
-- Returns:
- Throws:
IllegalArgumentException
- if the occurrences of element after this operation is bigger than Long.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 Long, 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(long occurrences, Throwables.Predicate<? super T, E> predicate) throws ERemoves the if.- Type Parameters:
E
-- Parameters:
occurrences
-predicate
-- Returns:
- Throws:
E
- the e
-
removeIf
public <E extends Exception> boolean removeIf(long occurrences, Throwables.BiPredicate<? super T, ? super Long, throws EE> predicate) Removes the if.- Type Parameters:
E
-- Parameters:
occurrences
-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, long 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 Long, throws EE> predicate, long newOccurrences) - Type Parameters:
E
-- Parameters:
predicate
-newOccurrences
-- Returns:
- Throws:
E
- the e
-
replaceAll
public <E extends Exception> void replaceAll(Throwables.BiFunction<? super T, ? super Long, throws ELong, 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
LongMultiset
-
filter
public <E extends Exception> LongMultiset<T> filter(Throwables.Predicate<? super T, E> filter) throws E- Type Parameters:
E
-- Parameters:
filter
-- Returns:
- Throws:
E
- the e
-
filter
public <E extends Exception> LongMultiset<T> filter(Throwables.BiPredicate<? super T, Long, 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> long computeIfAbsent(T e, Throwables.Function<? super T, Long, throws EE> mappingFunction) The implementation is equivalent to performing the following steps for this LongMultiset:final long oldValue = get(e); if (oldValue > 0) { return oldValue; } final long 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> long computeIfPresent(T e, Throwables.BiFunction<? super T, Long, throws ELong, E> remappingFunction) The implementation is equivalent to performing the following steps for this LongMultiset:final long oldValue = get(e); if (oldValue == 0) { return oldValue; } final long 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> long compute(T key, Throwables.BiFunction<? super T, Long, throws ELong, E> remappingFunction) The implementation is equivalent to performing the following steps for this LongMultiset:final long oldValue = get(key); final long 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> long merge(T key, long value, Throwables.BiFunction<Long, Long, throws ELong, E> remappingFunction) The implementation is equivalent to performing the following steps for this LongMultiset:long oldValue = get(key); long 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 LongMultiset<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 LongMultiset<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 LongMultiset<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 LongMultiset<T>, E> action) throws EAccept if not empty.- Type Parameters:
E
-- Parameters:
action
-- Returns:
- Throws:
E
- the e
-
hashCode
public int hashCode() -
equals
-
toString
-