Package org.apache.commons.collections4

# Interface MultiSet<E>

Type Parameters:
  `E` - The type held in the multiset

All Superinterfaces:
  `Collection<E>`, `Iterable<E>`

All Known Subinterfaces:
  `SortedMultiSet<E>`

All Known Implementing Classes:
  `AbstractMapMultiSet`, `AbstractMultiSet`, `AbstractMultiSetDecorator`, `AbstractSortedMultiSetDecorator`, `HashMultiSet`, `PredicatedMultiSet`, `PredicatedSortedMultiSet`, `SynchronizedMultiSet`, `SynchronizedSortedMultiSet`, `TransformedMultiSet`, `TransformedSortedMultiSet`, `TreeMultiSet`, `UnmodifiableMultiSet`, `UnmodifiableSortedMultiSet`

public interface MultiSet<E> extends Collection<E>
Defines a collection that counts the number of times an object appears in the collection.

Suppose you have a MultiSet that contains `{a, a, b, c}`. Calling `getCount(Object)` on `a` would return 2, while calling `uniqueSet()` would return `{a, b, c}`.

Since:
  4.1

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Interface
  Description
  `static interface`
  `MultiSet.Entry<E>`
  An unmodifiable entry for an element and its occurrence as contained in a MultiSet.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `add(E object)`
  Adds one copy of the specified object to the MultiSet.
  `int`
  `add(E object, int occurrences)`
  Adds a number of occurrences of the specified object to the MultiSet.
  `boolean`
  `containsAll(Collection<?> coll)`
  Returns `true` if the MultiSet contains at least one occurrence for each element contained in the given collection.
  `Set<MultiSet.Entry<E>>`
  `entrySet()`
  Returns a `Set` of all entries contained in the MultiSet.
  `boolean`
  `equals(Object obj)`
  Compares this MultiSet to another object.
  `int`
  `getCount(Object object)`
  Gets the number of occurrences of the given object currently in the MultiSet.
  `int`
  `hashCode()`
  Gets a hash code for the MultiSet compatible with the definition of equals.
  `Iterator<E>`
  `iterator()`
  Returns an `Iterator` over the entire set of members, including copies due to cardinality.
  `boolean`
  `remove(Object object)`
  Removes one occurrence of the given object from the MultiSet.
  `int`
  `remove(Object object, int occurrences)`
  Removes a number of occurrences of the specified object from the MultiSet.
  `boolean`
  `removeAll(Collection<?> coll)`
  Remove all occurrences of all elements from this MultiSet represented in the given collection.
  `boolean`
  `retainAll(Collection<?> coll)`
  Remove any elements of this MultiSet that are not contained in the given collection.
  `int`
  `setCount(E object, int count)`
  Sets the number of occurrences of the specified object in the MultiSet to the given count.
  `int`
  `size()`
  Returns the total number of items in the MultiSet.
  `Set<E>`
  `uniqueSet()`
  Returns a `Set` of unique elements in the MultiSet.

  ### Methods inherited from interface java.util.Collection

  `addAll, clear, contains, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArray`

  ### Methods inherited from interface java.lang.Iterable

  `forEach`

- ## Method Details

  - ### add

    boolean add(E object)
    Adds one copy of the specified object to the MultiSet.

    If the object is already in the `uniqueSet()` then increment its count as reported by `getCount(Object)`. Otherwise, add it to the `uniqueSet()` and report its count as 1.

    Specified by:
      `add` in interface `Collection<E>`
    Parameters:
      `object` - The object to add.
    Returns:
      `true` always, as the size of the MultiSet is increased in any case.

  - ### add

    int add(E object, int occurrences)
    Adds a number of occurrences of the specified object to the MultiSet.

    If the object is already in the `uniqueSet()` then increment its count as reported by `getCount(Object)`. Otherwise, add it to the `uniqueSet()` and report its count as `occurrences`.

    Parameters:
      `object` - The object to add.
      `occurrences` - The number of occurrences to add, may be zero, in which case no change is made to the multiset.
    Returns:
      The number of occurrences of the object in the multiset before this operation; possibly zero.
    Throws:
      `IllegalArgumentException` - if occurrences is negative.

  - ### containsAll

    boolean containsAll(Collection<?> coll)
    Returns `true` if the MultiSet contains at least one occurrence for each element contained in the given collection.
    Specified by:
      `containsAll` in interface `Collection<E>`
    Parameters:
      `coll` - The collection to check against.
    Returns:
      `true` if the MultiSet contains all the collection.

  - ### entrySet

    Set<MultiSet.Entry<E>> entrySet()
    Returns a `Set` of all entries contained in the MultiSet.

    The returned set is backed by this multiset, so any change to either is immediately reflected in the other.

    Returns:
      The Set of MultiSet entries.

  - ### equals

    boolean equals(Object obj)
    Compares this MultiSet to another object.

    This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.

    Specified by:
      `equals` in interface `Collection<E>`
    Overrides:
      `equals` in class `Object`
    Parameters:
      `obj` - The object to compare to.
    Returns:
      true if equal.

  - ### getCount

    int getCount(Object object)
    Gets the number of occurrences of the given object currently in the MultiSet. If the object does not exist in the multiset, return 0.
    Parameters:
      `object` - The object to search for.
    Returns:
      The number of occurrences of the object, zero if not found.

  - ### hashCode

    int hashCode()
    Gets a hash code for the MultiSet compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as `(e==null ? 0 : e.hashCode()) ^ noOccurrences)`.
    Specified by:
      `hashCode` in interface `Collection<E>`
    Overrides:
      `hashCode` in class `Object`
    Returns:
      The hash code of the MultiSet.

  - ### iterator

    Iterator<E> iterator()
    Returns an `Iterator` over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.
    Specified by:
      `iterator` in interface `Collection<E>`
    Specified by:
      `iterator` in interface `Iterable<E>`
    Returns:
      iterator over all elements in the MultiSet.

  - ### remove

    boolean remove(Object object)
    Removes one occurrence of the given object from the MultiSet.

    If the number of occurrences after this operation is reduced to zero, the object will be removed from the `uniqueSet()`.

    Specified by:
      `remove` in interface `Collection<E>`
    Parameters:
      `object` - The object to remove.
    Returns:
      `true` if this call changed the collection.

  - ### remove

    int remove(Object object, int occurrences)
    Removes a number of occurrences of the specified object from the MultiSet.

    If the number of occurrences to remove is greater than the actual number of occurrences in the multiset, the object will be removed from the multiset.

    Parameters:
      `object` - The object to remove.
      `occurrences` - The number of occurrences to remove, may be zero, in which case no change is made to the multiset.
    Returns:
      The number of occurrences of the object in the multiset before the operation; possibly zero.
    Throws:
      `IllegalArgumentException` - if occurrences is negative.

  - ### removeAll

    boolean removeAll(Collection<?> coll)
    Remove all occurrences of all elements from this MultiSet represented in the given collection.
    Specified by:
      `removeAll` in interface `Collection<E>`
    Parameters:
      `coll` - The collection of elements to remove.
    Returns:
      `true` if this call changed the multiset.

  - ### retainAll

    boolean retainAll(Collection<?> coll)
    Remove any elements of this MultiSet that are not contained in the given collection.
    Specified by:
      `retainAll` in interface `Collection<E>`
    Parameters:
      `coll` - The collection of elements to retain.
    Returns:
      `true` if this call changed the multiset.

  - ### setCount

    int setCount(E object, int count)
    Sets the number of occurrences of the specified object in the MultiSet to the given count.

    If the provided count is zero, the object will be removed from the `uniqueSet()`.

    Parameters:
      `object` - The object to update.
      `count` - The number of occurrences of the object.
    Returns:
      The number of occurrences of the object before this operation, zero if the object was not contained in the multiset.
    Throws:
      `IllegalArgumentException` - if count is negative.

  - ### size

    int size()
    Returns the total number of items in the MultiSet.
    Specified by:
      `size` in interface `Collection<E>`
    Returns:
      The total size of the multiset.

  - ### uniqueSet

    Set<E> uniqueSet()
    Returns a `Set` of unique elements in the MultiSet.

    Uniqueness constraints are the same as those in `Set`.

    The returned set is backed by this multiset, so any change to either is immediately reflected in the other. Only removal operations are supported, in which case all occurrences of the element are removed from the backing multiset.

    Returns:
      The Set of unique MultiSet elements.