Class ImmutableMultiset.Builder<E>

  • Direct Known Subclasses:
    ImmutableSortedMultiset.Builder
    Enclosing class:
    ImmutableMultiset<E>

    public static class ImmutableMultiset.Builder<E>
    extends ImmutableCollection.Builder<E>
    A builder for creating immutable multiset instances, especially public static final multisets ("constant multisets"). Example:
    
     public static final ImmutableMultiset<Bean> BEANS =
         new ImmutableMultiset.Builder<Bean>()
             .addCopies(Bean.COCOA, 4)
             .addCopies(Bean.GARDEN, 6)
             .addCopies(Bean.RED, 8)
             .addCopies(Bean.BLACK_EYED, 10)
             .build();
     

    Builder instances can be reused; it is safe to call build() multiple times to build multiple multisets in series.

    Since:
    2.0
    • Method Detail

      • addCopies

        @CanIgnoreReturnValue
        public ImmutableMultiset.Builder<EaddCopies​(E element,
                                                      int occurrences)
        Adds a number of occurrences of an element to this ImmutableMultiset.
        Parameters:
        element - the element to add
        occurrences - the number of occurrences of the element to add. May be zero, in which case no change will be made.
        Returns:
        this Builder object
        Throws:
        java.lang.NullPointerException - if element is null
        java.lang.IllegalArgumentException - if occurrences is negative, or if this operation would result in more than Integer.MAX_VALUE occurrences of the element
      • setCount

        @CanIgnoreReturnValue
        public ImmutableMultiset.Builder<EsetCount​(E element,
                                                     int count)
        Adds or removes the necessary occurrences of an element such that the element attains the desired count.
        Parameters:
        element - the element to add or remove occurrences of
        count - the desired count of the element in this multiset
        Returns:
        this Builder object
        Throws:
        java.lang.NullPointerException - if element is null
        java.lang.IllegalArgumentException - if count is negative