Package org.apache.commons.collections4

# Interface Bag<E>

Type Parameters:
  `E` - The type of elements in this bag

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

All Known Subinterfaces:
  `SortedBag<E>`

All Known Implementing Classes:
  `AbstractBagDecorator`, `AbstractMapBag`, `AbstractSortedBagDecorator`, `CollectionBag`, `CollectionSortedBag`, `HashBag`, `PredicatedBag`, `PredicatedSortedBag`, `SynchronizedBag`, `SynchronizedSortedBag`, `TransformedBag`, `TransformedSortedBag`, `TreeBag`, `UnmodifiableBag`, `UnmodifiableSortedBag`

@Deprecated public interface Bag<E> extends Collection<E>
Deprecated.
Since 4.6.0, use `MultiSet` instead; see the migration notes above.
Defines a collection that counts the number of times an object appears in the collection.

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

NOTE: This interface violates the `Collection` contract. The behavior specified in many of these methods is not the same as the behavior specified by `Collection`. The non-compliant methods are clearly marked with "(Violation)". Exercise caution when using a bag as a `Collection`.

This violation resulted from the original specification of this interface. In an ideal world, the interface would be changed to fix the problems, however it has been decided to maintain backwards compatibility instead.

The `MultiSet` interface, added in version 4.1, provides the same functionality while complying with the `Collection` contract, and should be preferred for new code. Existing code can migrate as follows, with the cardinality-respecting behavior preserved through explicitly named methods:

- `bag.add(e)` becomes `multiSet.add(e)` (which always returns `true`, per the `MultiSet` contract)
- `bag.getCount(e)` becomes `multiSet.getCount(e)`
- `bag.remove(e)`, which removes all copies, becomes `multiSet.setCount(e, 0)`
- `bag.containsAll(coll)` becomes `MultiSetUtils.containsOccurrences(multiSet, new HashMultiSet<>(coll))`
- `bag.removeAll(coll)` becomes `MultiSetUtils.removeOccurrences(multiSet, new HashMultiSet<>(coll))`
- `bag.retainAll(coll)` becomes `MultiSetUtils.retainOccurrences(multiSet, new HashMultiSet<>(coll))`
- `SortedBag` and `TreeBag` become `SortedMultiSet` and `TreeMultiSet`
- the `CollectionBag` and `CollectionSortedBag` wrappers are not needed, as a `MultiSet` already complies with the `Collection` contract

Since:
  2.0
See Also:
  - `MultiSet`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `add(E object)`
  Deprecated.
  (Violation) Adds one copy of the specified object to the Bag.
  `boolean`
  `add(E object, int nCopies)`
  Deprecated.
  Adds `nCopies` copies of the specified object to the Bag.
  `boolean`
  `containsAll(Collection<?> coll)`
  Deprecated.
  (Violation) Returns `true` if the bag contains all elements in the given collection, respecting cardinality.
  `int`
  `getCount(Object object)`
  Deprecated.
  Gets the number of occurrences (cardinality) of the given object currently in the bag.
  `Iterator<E>`
  `iterator()`
  Deprecated.
  Returns an `Iterator` over the entire set of members, including copies due to cardinality.
  `boolean`
  `remove(Object object)`
  Deprecated.
  (Violation) Removes all occurrences of the given object from the bag.
  `boolean`
  `remove(Object object, int nCopies)`
  Deprecated.
  Removes `nCopies` copies of the specified object from the Bag.
  `boolean`
  `removeAll(Collection<?> coll)`
  Deprecated.
  (Violation) Remove all elements represented in the given collection, respecting cardinality.
  `boolean`
  `retainAll(Collection<?> coll)`
  Deprecated.
  (Violation) Remove any members of the bag that are not in the given collection, respecting cardinality.
  `int`
  `size()`
  Deprecated.
  Returns the total number of items in the bag across all types.
  `Set<E>`
  `uniqueSet()`
  Deprecated.
  Returns a `Set` of unique elements in the Bag.

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

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

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

  `forEach`

- ## Method Details

  - ### add

    boolean add(E object)
    Deprecated.
    (Violation) Adds one copy of the specified object to the Bag.

    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.

    Since this method always increases the size of the bag, according to the `Collection.add(Object)` contract, it should always return `true`. Since it sometimes returns `false`, this method violates the contract.

    Specified by:
      `add` in interface `Collection<E>`
    Parameters:
      `object` - The object to add.
    Returns:
      `true` if the object was not already in the `uniqueSet`.

  - ### add

    boolean add(E object, int nCopies)
    Deprecated.
    Adds `nCopies` copies of the specified object to the Bag.

    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 `nCopies`.

    Parameters:
      `object` - The object to add.
      `nCopies` - The number of copies to add.
    Returns:
      `true` if the object was not already in the `uniqueSet`.
    Throws:
      `ClassCastException` - if the class of the specified element prevents it from being added to this collection.

  - ### containsAll

    boolean containsAll(Collection<?> coll)
    Deprecated.
    (Violation) Returns `true` if the bag contains all elements in the given collection, respecting cardinality. That is, if the given collection `coll` contains `n` copies of a given object, calling `getCount(Object)` on that object must be `&gt;= n` for all `n` in `coll`.

    The `Collection.containsAll(Collection)` method specifies that cardinality should not be respected; this method should return true if the bag contains at least one of every object contained in the given collection.

    Specified by:
      `containsAll` in interface `Collection<E>`
    Parameters:
      `coll` - The collection to check against.
    Returns:
      `true` if the Bag contains all the collection.

  - ### getCount

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

  - ### iterator

    Iterator<E> iterator()
    Deprecated.
    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 Bag.

  - ### remove

    boolean remove(Object object)
    Deprecated.
    (Violation) Removes all occurrences of the given object from the bag.

    This will also remove the object from the `uniqueSet()`.

    According to the `Collection.remove(Object)` method, this method should only remove the first occurrence of the given object, not all occurrences.

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

  - ### remove

    boolean remove(Object object, int nCopies)
    Deprecated.
    Removes `nCopies` copies of the specified object from the Bag.

    If the number of copies to remove is greater than the actual number of copies in the Bag, no error is thrown.

    Parameters:
      `object` - The object to remove.
      `nCopies` - The number of copies to remove.
    Returns:
      `true` if this call changed the collection.

  - ### removeAll

    boolean removeAll(Collection<?> coll)
    Deprecated.
    (Violation) Remove all elements represented in the given collection, respecting cardinality. That is, if the given collection `coll` contains `n` copies of a given object, the bag will have `n` fewer copies, assuming the bag had at least `n` copies to begin with.

    The `Collection.removeAll(Collection)` method specifies that cardinality should not be respected; this method should remove all occurrences of every object contained in the given collection.

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

  - ### retainAll

    boolean retainAll(Collection<?> coll)
    Deprecated.
    (Violation) Remove any members of the bag that are not in the given collection, respecting cardinality. That is, if the given collection `coll` contains `n` copies of a given object and the bag has `m > n` copies, then delete `m - n` copies from the bag. In addition, if `e` is an object in the bag but `!coll.contains(e)`, then remove `e` and any of its copies.

    The `Collection.retainAll(Collection)` method specifies that cardinality should not be respected; this method should keep all occurrences of every object contained in the given collection.

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

  - ### size

    int size()
    Deprecated.
    Returns the total number of items in the bag across all types.
    Specified by:
      `size` in interface `Collection<E>`
    Returns:
      The total size of the Bag.

  - ### uniqueSet

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

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

    Returns:
      The Set of unique Bag elements.