com.google.common.collect

## Class ImmutableMultiset<E>

- java.lang.Object
- - java.util.AbstractCollection<E>
  - - com.google.common.collect.ImmutableCollection<E>
    - - com.google.common.collect.ImmutableMultiset<E>

- All Implemented Interfaces:
    Multiset<E>, java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>
  Direct Known Subclasses:
    ImmutableSortedMultiset

      @GwtCompatible(serializable=true,
                     emulated=true)
      public abstract class ImmutableMultiset<E>
      extends ImmutableCollection<E>
      implements Multiset<E>

  A `Multiset` whose contents will never change, with many other important properties detailed at `ImmutableCollection`.

  Grouped iteration. In all current implementations, duplicate elements always appear consecutively when iterating. Elements iterate in order by the first appearance of that element when the multiset was created.

  See the Guava User Guide article on immutable collections.

  Since:
    2.0
  See Also:
    Serialized Form

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `ImmutableMultiset.Builder<E>` A builder for creating immutable multiset instances, especially `public static final` multisets ("constant multisets").

    - ### Nested classes/interfaces inherited from interface com.google.common.collect.Multiset

      `Multiset.Entry<E>`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `add(E element, int occurrences)` Deprecated. Unsupported operation.
    `static <E> ImmutableMultiset.Builder<E>` | `builder()` Returns a new builder.
    `boolean` | `contains(java.lang.Object object)` Determines whether this multiset contains the specified element.
    `static <E> ImmutableMultiset<E>` | `copyOf(E[] elements)` Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
    `static <E> ImmutableMultiset<E>` | `copyOf(java.lang.Iterable<? extends E> elements)` Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
    `static <E> ImmutableMultiset<E>` | `copyOf(java.util.Iterator<? extends E> elements)` Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
    `ImmutableSet<Multiset.Entry<E>>` | `entrySet()` Returns a view of the contents of this multiset, grouped into `Multiset.Entry` instances, each providing an element of the multiset and the count of that element.
    `boolean` | `equals(java.lang.Object object)` Compares the specified object with this multiset for equality.
    `int` | `hashCode()` Returns the hash code for this multiset.
    `UnmodifiableIterator<E>` | `iterator()` Returns an unmodifiable iterator across the elements in this collection.
    `static <E> ImmutableMultiset<E>` | `of()` Returns the empty immutable multiset.
    `static <E> ImmutableMultiset<E>` | `of(E element)` Returns an immutable multiset containing a single element.
    `static <E> ImmutableMultiset<E>` | `of(E e1, E e2)` Returns an immutable multiset containing the given elements, in order.
    `static <E> ImmutableMultiset<E>` | `of(E e1, E e2, E e3)` Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
    `static <E> ImmutableMultiset<E>` | `of(E e1, E e2, E e3, E e4)` Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
    `static <E> ImmutableMultiset<E>` | `of(E e1, E e2, E e3, E e4, E e5)` Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
    `static <E> ImmutableMultiset<E>` | `of(E e1, E e2, E e3, E e4, E e5, E e6, E... others)` Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
    `int` | `remove(java.lang.Object element, int occurrences)` Deprecated. Unsupported operation.
    `int` | `setCount(E element, int count)` Deprecated. Unsupported operation.
    `boolean` | `setCount(E element, int oldCount, int newCount)` Deprecated. Unsupported operation.
    `java.lang.String` | `toString()`

    - ### Methods inherited from class com.google.common.collect.ImmutableCollection

      `add, addAll, asList, clear, remove, removeAll, retainAll, toArray, toArray`

    - ### Methods inherited from class java.util.AbstractCollection

      `containsAll, isEmpty, size`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface com.google.common.collect.Multiset

      `add, containsAll, count, elementSet, remove, removeAll, retainAll`

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

      `addAll, clear, isEmpty, size, toArray, toArray`

- - ### Method Detail

    - #### of

          public static <E> ImmutableMultiset<E> of()

      Returns the empty immutable multiset.

    - #### of

          public static <E> ImmutableMultiset<E> of(E element)

      Returns an immutable multiset containing a single element.
      Throws:
        `java.lang.NullPointerException` - if `element` is null
      Since:
        6.0 (source-compatible since 2.0)

    - #### of

          public static <E> ImmutableMultiset<E> of(E e1,
                                    E e2)

      Returns an immutable multiset containing the given elements, in order.
      Throws:
        `java.lang.NullPointerException` - if any element is null
      Since:
        6.0 (source-compatible since 2.0)

    - #### of

          public static <E> ImmutableMultiset<E> of(E e1,
                                    E e2,
                                    E e3)

      Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
      Throws:
        `java.lang.NullPointerException` - if any element is null
      Since:
        6.0 (source-compatible since 2.0)

    - #### of

          public static <E> ImmutableMultiset<E> of(E e1,
                                    E e2,
                                    E e3,
                                    E e4)

      Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
      Throws:
        `java.lang.NullPointerException` - if any element is null
      Since:
        6.0 (source-compatible since 2.0)

    - #### of

          public static <E> ImmutableMultiset<E> of(E e1,
                                    E e2,
                                    E e3,
                                    E e4,
                                    E e5)

      Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
      Throws:
        `java.lang.NullPointerException` - if any element is null
      Since:
        6.0 (source-compatible since 2.0)

    - #### of

          public static <E> ImmutableMultiset<E> of(E e1,
                                    E e2,
                                    E e3,
                                    E e4,
                                    E e5,
                                    E e6,
                                    E... others)

      Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
      Throws:
        `java.lang.NullPointerException` - if any element is null
      Since:
        6.0 (source-compatible since 2.0)

    - #### copyOf

          public static <E> ImmutableMultiset<E> copyOf(E[] elements)

      Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
      Throws:
        `java.lang.NullPointerException` - if any of `elements` is null
      Since:
        6.0

    - #### copyOf

          public static <E> ImmutableMultiset<E> copyOf(java.lang.Iterable<? extends E> elements)

      Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
      Throws:
        `java.lang.NullPointerException` - if any of `elements` is null

    - #### copyOf

          public static <E> ImmutableMultiset<E> copyOf(java.util.Iterator<? extends E> elements)

      Returns an immutable multiset containing the given elements, in the "grouped iteration order" described in the class documentation.
      Throws:
        `java.lang.NullPointerException` - if any of `elements` is null

    - #### iterator

          public UnmodifiableIterator<E> iterator()

      Description copied from class: `ImmutableCollection`
      Returns an unmodifiable iterator across the elements in this collection.
      Specified by:
        `iterator` in interface `Multiset<E>`
      Specified by:
        `iterator` in interface `java.lang.Iterable<E>`
      Specified by:
        `iterator` in interface `java.util.Collection<E>`
      Specified by:
        `iterator` in class `ImmutableCollection<E>`

    - #### contains

          public boolean contains(@Nullable
                         java.lang.Object object)

      Description copied from interface: `Multiset`
      Determines whether this multiset contains the specified element.

      This method refines `Collection.contains(java.lang.Object)` to further specify that it may not throw an exception in response to `element` being null or of the wrong type.

      Specified by:
        `contains` in interface `Multiset<E>`
      Specified by:
        `contains` in interface `java.util.Collection<E>`
      Specified by:
        `contains` in class `ImmutableCollection<E>`
      Parameters:
        `object` - the element to check for
      Returns:
        `true` if this multiset contains at least one occurrence of the element

    - #### add

          @Deprecated
          public final int add(E element,
                           int occurrences)

      Deprecated. Unsupported operation.
      Guaranteed to throw an exception and leave the collection unmodified.
      Specified by:
        `add` in interface `Multiset<E>`
      Parameters:
        `element` - the element to add occurrences of; may be null only if explicitly allowed by the implementation
        `occurrences` - the number of occurrences of the element to add. May be zero, in which case no change will be made.
      Returns:
        the count of the element before the operation; possibly zero
      Throws:
        `java.lang.UnsupportedOperationException` - always

    - #### remove

          @Deprecated
          public final int remove(java.lang.Object element,
                              int occurrences)

      Deprecated. Unsupported operation.
      Guaranteed to throw an exception and leave the collection unmodified.
      Specified by:
        `remove` in interface `Multiset<E>`
      Parameters:
        `element` - the element to conditionally remove occurrences of
        `occurrences` - the number of occurrences of the element to remove. May be zero, in which case no change will be made.
      Returns:
        the count of the element before the operation; possibly zero
      Throws:
        `java.lang.UnsupportedOperationException` - always

    - #### setCount

          @Deprecated
          public final int setCount(E element,
                                int count)

      Deprecated. Unsupported operation.
      Guaranteed to throw an exception and leave the collection unmodified.
      Specified by:
        `setCount` in interface `Multiset<E>`
      Parameters:
        `element` - the element to add or remove occurrences of; may be null only if explicitly allowed by the implementation
        `count` - the desired count of the element in this multiset
      Returns:
        the count of the element before the operation; possibly zero
      Throws:
        `java.lang.UnsupportedOperationException` - always

    - #### setCount

          @Deprecated
          public final boolean setCount(E element,
                                    int oldCount,
                                    int newCount)

      Deprecated. Unsupported operation.
      Guaranteed to throw an exception and leave the collection unmodified.
      Specified by:
        `setCount` in interface `Multiset<E>`
      Parameters:
        `element` - the element to conditionally set the count of; may be null only if explicitly allowed by the implementation
        `oldCount` - the expected present count of the element in this multiset
        `newCount` - the desired count of the element in this multiset
      Returns:
        `true` if the condition for modification was met. This implies that the multiset was indeed modified, unless `oldCount == newCount`.
      Throws:
        `java.lang.UnsupportedOperationException` - always

    - #### equals

          public boolean equals(@Nullable
                       java.lang.Object object)

      Description copied from interface: `Multiset`
      Compares the specified object with this multiset for equality. Returns `true` if the given object is also a multiset and contains equal elements with equal counts, regardless of order.
      Specified by:
        `equals` in interface `Multiset<E>`
      Specified by:
        `equals` in interface `java.util.Collection<E>`
      Overrides:
        `equals` in class `java.lang.Object`

    - #### hashCode

          public int hashCode()

      Description copied from interface: `Multiset`
      Returns the hash code for this multiset. This is defined as the sum of

             ((element == null) ? 0 : element.hashCode()) ^ count(element)

      over all distinct elements in the multiset. It follows that a multiset and its entry set always have the same hash code.

      Specified by:
        `hashCode` in interface `Multiset<E>`
      Specified by:
        `hashCode` in interface `java.util.Collection<E>`
      Overrides:
        `hashCode` in class `java.lang.Object`

    - #### toString

          public java.lang.String toString()

      Description copied from interface: `Multiset`

      It is recommended, though not mandatory, that this method return the result of invoking `Multiset.toString()` on the `Multiset.entrySet()`, yielding a result such as `[a x 3, c, d x 2, e]`.

      Specified by:
        `toString` in interface `Multiset<E>`
      Overrides:
        `toString` in class `java.util.AbstractCollection<E>`

    - #### entrySet

          public ImmutableSet<Multiset.Entry<E>> entrySet()

      Description copied from interface: `Multiset`
      Returns a view of the contents of this multiset, grouped into `Multiset.Entry` instances, each providing an element of the multiset and the count of that element. This set contains exactly one entry for each distinct element in the multiset (thus it always has the same size as the `Multiset.elementSet()`). The order of the elements in the element set is unspecified.

      The entry set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. However, multiset changes may or may not be reflected in any `Entry` instances already retrieved from the entry set (this is implementation-dependent). Furthermore, implementations are not required to support modifications to the entry set at all, and the `Entry` instances themselves don't even have methods for modification. See the specific implementation class for more details on how its entry set handles modifications.

      Specified by:
        `entrySet` in interface `Multiset<E>`
      Returns:
        a set of entries representing the data of this multiset

    - #### builder

          public static <E> ImmutableMultiset.Builder<E> builder()

      Returns a new builder. The generated builder is equivalent to the builder created by the `ImmutableMultiset.Builder` constructor.