Class LongMultiset<T>

java.lang.Object
com.landawn.abacus.util.LongMultiset<T>
Type Parameters:
T -
All Implemented Interfaces:
Iterable<T>

public final class LongMultiset<T> extends Object implements 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 Details

    • LongMultiset

      public LongMultiset()
    • LongMultiset

      public LongMultiset(int initialCapacity)
      Parameters:
      initialCapacity -
    • LongMultiset

      public LongMultiset(Collection<? extends T> c)
      Parameters:
      c -
    • LongMultiset

      public LongMultiset(Class<? extends Map> valueMapType)
      Parameters:
      valueMapType -
    • LongMultiset

      public LongMultiset(Supplier<? extends Map<? extends T,?>> mapSupplier)
      Parameters:
      mapSupplier -
  • Method Details

    • of

      @SafeVarargs public static <T> LongMultiset<T> of(T... a)
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
    • create

      public static <T> LongMultiset<T> create(Collection<? extends T> coll)
      Type Parameters:
      T -
      Parameters:
      coll -
      Returns:
    • create

      public static <T> LongMultiset<T> create(Iterator<? extends T> iter)
      Type Parameters:
      T -
      Parameters:
      iter -
      Returns:
    • create

      public static <T> LongMultiset<T> create(Map<? extends T,Long> m)
      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

      public static <T> LongMultiset<T> create(Multiset<? extends T> multiset)
      Type Parameters:
      T -
      Parameters:
      multiset -
      Returns:
    • get

      public long get(Object e)
      Parameters:
      e -
      Returns:
    • getOrDefault

      public long getOrDefault(Object e, long defaultValue)
      Gets the or default.
      Parameters:
      e -
      defaultValue -
      Returns:
    • getAndSet

      public long getAndSet(T e, long occurrences)
      The element will be removed if the specified count is 0.
      Parameters:
      e -
      occurrences -
      Returns:
    • setAndGet

      public long setAndGet(T e, long occurrences)
      The element will be removed if the specified count is 0.
      Parameters:
      e -
      occurrences -
      Returns:
    • set

      public LongMultiset<T> set(T e, long occurrences)
      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

      public LongMultiset<T> setAll(Collection<? extends T> c, long occurrences)
      Sets the all.
      Parameters:
      c -
      occurrences -
      Returns:
    • setAll

      public LongMultiset<T> setAll(Map<? extends T,Long> m) throws IllegalArgumentException
      Sets the all.
      Parameters:
      m -
      Returns:
      this LongMultiset.
      Throws:
      IllegalArgumentException - if the occurrences of element is less than 0.
    • setAll

      public LongMultiset<T> setAll(LongMultiset<? extends T> multiset) throws IllegalArgumentException
      Sets the all.
      Parameters:
      multiset -
      Returns:
      this LongMultiset.
      Throws:
      IllegalArgumentException - if the occurrences of element is less than 0.
    • occurrencesOf

      public long occurrencesOf(Object e)
      Parameters:
      e -
      Returns:
    • minOccurrences

      public u.Optional<Pair<Long,T>> minOccurrences()
      Returns:
    • maxOccurrences

      public u.Optional<Pair<Long,T>> maxOccurrences()
      Returns:
    • allMinOccurrences

      public u.Optional<Pair<Long,List<T>>> allMinOccurrences()
      All min occurrences.
      Returns:
    • allMaxOccurrences

      public u.Optional<Pair<Long,List<T>>> 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

      public u.OptionalDouble averageOfOccurrences()
      Average of occurrences.
      Returns:
    • add

      public boolean add(T e) throws IllegalArgumentException
      Parameters:
      e -
      Returns:
      always true
      Throws:
      IllegalArgumentException - if the occurrences of element after this operation is bigger than Long.MAX_VALUE.
    • add

      public boolean add(T e, long occurrencesToAdd) throws IllegalArgumentException
      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

      public boolean addIfAbsent(T e) throws IllegalArgumentException
      Adds the if absent.
      Parameters:
      e -
      Returns:
      true if the specified element is absent.
      Throws:
      IllegalArgumentException - the illegal argument exception
    • addIfAbsent

      public boolean addIfAbsent(T e, long occurrencesToAdd) throws IllegalArgumentException
      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

      public long addAndGet(T e)
      Adds the and get.
      Parameters:
      e -
      Returns:
    • addAndGet

      public long addAndGet(T e, long occurrencesToAdd)
      Adds the and get.
      Parameters:
      e -
      occurrencesToAdd -
      Returns:
    • getAndAdd

      public long getAndAdd(T e)
      Gets the and add.
      Parameters:
      e -
      Returns:
    • getAndAdd

      public long getAndAdd(T e, long occurrencesToAdd)
      Gets the and add.
      Parameters:
      e -
      occurrencesToAdd -
      Returns:
    • addAll

      public boolean addAll(Collection<? extends T> c) throws IllegalArgumentException
      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

      public boolean addAll(Map<? extends T,Long> m) throws IllegalArgumentException
      Adds the all.
      Parameters:
      m -
      Returns:
      Throws:
      IllegalArgumentException - if the occurrences of element after this operation is bigger than Long.MAX_VALUE.
    • addAll

      public boolean addAll(LongMultiset<? extends T> multiset) throws IllegalArgumentException
      Adds the all.
      Parameters:
      multiset -
      Returns:
      Throws:
      IllegalArgumentException - if the occurrences of element is less than 0.
    • contains

      public boolean contains(Object o)
      Parameters:
      o -
      Returns:
    • containsAll

      public boolean containsAll(Collection<?> c)
      Parameters:
      c -
      Returns:
    • remove

      public boolean remove(Object e)
      Remove one occurrence from the specified elements. The element will be removed from this Multiset if the occurrences equals to or less than 0 after the operation.
      Parameters:
      e -
      Returns:
    • remove

      public boolean remove(Object e, long occurrencesToRemove)
      Remove the specified occurrences from the specified element. The element will be removed from this Multiset if the occurrences equals to or less than 0 after the operation.
      Parameters:
      e -
      occurrencesToRemove -
      Returns:
    • removeAndGet

      public long removeAndGet(Object e)
      Removes the and get.
      Parameters:
      e -
      Returns:
    • removeAndGet

      public long removeAndGet(Object e, long occurrencesToRemove)
      Removes the and get.
      Parameters:
      e -
      occurrencesToRemove -
      Returns:
    • getAndRemove

      public long getAndRemove(Object e)
      Gets the and remove.
      Parameters:
      e -
      Returns:
    • getAndRemove

      public long getAndRemove(Object e, long occurrencesToRemove)
      Gets the and remove.
      Parameters:
      e -
      occurrencesToRemove -
      Returns:
    • removeAllOccurrences

      public long removeAllOccurrences(Object e)
      Removes the all occurrences.
      Parameters:
      e -
      Returns:
    • removeAllOccurrencesIf

      public <E extends Exception> boolean removeAllOccurrencesIf(Throwables.Predicate<? super T,E> predicate) throws E
      Removes 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,E> predicate) throws E
      Removes the all occurrences if.
      Type Parameters:
      E -
      Parameters:
      predicate -
      Returns:
      Throws:
      E - the e
    • removeAllOccurrencesForEach

      public boolean removeAllOccurrencesForEach(Collection<?> c)
      Removes the all occurrences.
      Parameters:
      c -
      Returns:
    • removeIf

      public <E extends Exception> boolean removeIf(long occurrences, Throwables.Predicate<? super T,E> predicate) throws E
      Removes 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,E> predicate) throws E
      Removes the if.
      Type Parameters:
      E -
      Parameters:
      occurrences -
      predicate -
      Returns:
      Throws:
      E - the e
    • removeAll

      public boolean removeAll(Collection<?> c)
      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

      public boolean removeAll(Collection<?> c, long occurrencesToRemove)
      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 collection c.
      Returns:
      true if this set changed as a result of the call
    • removeAll

      public boolean removeAll(Map<?,Long> m)
      Removes the all.
      Parameters:
      m -
      Returns:
    • removeAll

      public boolean removeAll(LongMultiset<?> multiset) throws IllegalArgumentException
      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,E> predicate, long newOccurrences) throws E
      Type Parameters:
      E -
      Parameters:
      predicate -
      newOccurrences -
      Returns:
      Throws:
      E - the e
    • replaceAll

      public <E extends Exception> void replaceAll(Throwables.BiFunction<? super T,? super Long,Long,E> function) throws E
      The associated elements will be removed if zero or negative occurrences are returned by the specified function.
      Type Parameters:
      E -
      Parameters:
      function -
      Throws:
      E - the e
    • retainAll

      public boolean retainAll(Collection<?> c)
      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

      public LongMultiset<T> copy()
      Returns:
    • elements

      public ImmutableSet<T> 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

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
      Returns:
    • flatIterator

      public Iterator<T> flatIterator()
      Returns:
    • toArray

      public Object[] toArray()
      Returns:
    • toArray

      public <A> A[] toArray(A[] a)
      Type Parameters:
      A -
      Parameters:
      a -
      Returns:
    • toMap

      public Map<T,Long> toMap()
      Returns:
    • toMap

      public <M extends Map<T, Long>> M toMap(IntFunction<? extends M> supplier)
      Type Parameters:
      M -
      Parameters:
      supplier -
      Returns:
    • toMapSortedByOccurrences

      public Map<T,Long> toMapSortedByOccurrences()
      To map sorted by occurrences.
      Returns:
    • toMapSortedByOccurrences

      public Map<T,Long> toMapSortedByOccurrences(Comparator<? super Long> cmp)
      To map sorted by occurrences.
      Parameters:
      cmp -
      Returns:
    • toMapSortedByKey

      public Map<T,Long> toMapSortedByKey(Comparator<? super T> cmp)
      To map sorted by key.
      Parameters:
      cmp -
      Returns:
    • toImmutableMap

      public ImmutableMap<T,Long> toImmutableMap()
      To immutable map.
      Returns:
    • toImmutableMap

      public ImmutableMap<T,Long> toImmutableMap(IntFunction<? extends Map<T,Long>> mapSupplier)
      To immutable map.
      Parameters:
      mapSupplier -
      Returns:
    • flatten

      public List<T> 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,E> filter) throws E
      Type Parameters:
      E -
      Parameters:
      filter -
      Returns:
      Throws:
      E - the e
    • forEach

      public <E extends Exception> void forEach(Throwables.Consumer<? super T,E> action) throws E
      Type Parameters:
      E -
      Parameters:
      action -
      Throws:
      E - the e
    • forEach

      public <E extends Exception> void forEach(Throwables.ObjLongConsumer<? super T,E> action) throws E
      Type Parameters:
      E -
      Parameters:
      action -
      Throws:
      E - the e
    • computeIfAbsent

      public <E extends Exception> long computeIfAbsent(T e, Throwables.Function<? super T,Long,E> mappingFunction) throws E
      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,Long,E> remappingFunction) throws E
      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,Long,E> remappingFunction) throws E
      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,Long,E> remappingFunction) throws E
      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

      public Stream<T> stream()
      Returns:
    • flatStream

      public Stream<T> flatStream()
      Returns:
    • entryStream

      public EntryStream<T,Long> entryStream()
      Returns:
    • apply

      public <R, E extends Exception> R apply(Throwables.Function<? super LongMultiset<T>,? extends R,E> func) throws E
      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,E> func) throws E
      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 E
      Accept if not empty.
      Type Parameters:
      E -
      Parameters:
      action -
      Returns:
      Throws:
      E - the e
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      Returns:
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      Parameters:
      obj -
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns: