Class Multiset<T>

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

public final class Multiset<T> extends Object implements 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 MutableInt#MAX_VALUE occurrences of any one element.

Since:
0.8
Author:
Haiyang Li
  • Constructor Details

    • Multiset

      public Multiset()
    • Multiset

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

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

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

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

    • of

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      public u.Optional<Pair<Integer,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 int.
    • 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 Integer.MAX_VALUE.
    • add

      public boolean add(T e, int 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 Integer.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, int 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 int addAndGet(T e)
      Adds the and get.
      Parameters:
      e -
      Returns:
    • addAndGet

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

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

      public int getAndAdd(T e, int 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 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

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

      public boolean addAll(Multiset<? 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, int 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 int removeAndGet(Object e)
      Removes the and get.
      Parameters:
      e -
      Returns:
    • removeAndGet

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

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

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

      public int 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 Integer,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(int occurrencesToRemove, Throwables.Predicate<? super T,E> predicate) throws E
      Removes 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,E> predicate) throws E
      Removes the if.
      Type Parameters:
      E -
      Parameters:
      occurrencesToRemove -
      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, int 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<?,Integer> m)
      Removes the all.
      Parameters:
      m -
      Returns:
    • removeAll

      public boolean removeAll(Multiset<?> 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, 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,E> predicate, int 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 Integer,Integer,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 Multiset<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,Integer> toMap()
      Returns:
    • toMap

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

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

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

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

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

      public ImmutableMap<T,Integer> toImmutableMap(IntFunction<? extends Map<T,Integer>> 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 Multiset
    • filter

      public <E extends Exception> Multiset<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> Multiset<T> filter(Throwables.BiPredicate<? super T,Integer,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.ObjIntConsumer<? super T,E> action) throws E
      Type Parameters:
      E -
      Parameters:
      action -
      Throws:
      E - the e
    • computeIfAbsent

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

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

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

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

      public <R, E extends Exception> R apply(Throwables.Function<? super Multiset<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 Multiset<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 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 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: