Class Multisets

java.lang.Object
org.docx4j.com.google.common.collect.Multisets

@GwtCompatible
public final class Multisets
extends java.lang.Object
Provides static utility methods for creating and working with Multiset instances.

See the Guava User Guide article on Multisets.

Since:
2.0
Author:
Kevin Bourrillion, Mike Bostock, Louis Wasserman
  • Method Summary

    Modifier and Type Method Description
    static boolean containsOccurrences​(Multiset<?> superMultiset, Multiset<?> subMultiset)
    Returns true if subMultiset.count(o) <= superMultiset.count(o) for all o.
    static boolean removeOccurrences​(Multiset<?> multisetToModify, java.lang.Iterable<?> occurrencesToRemove)
    For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.
    static boolean removeOccurrences​(Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove)
    For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.
    static boolean retainOccurrences​(Multiset<?> multisetToModify, Multiset<?> multisetToRetain)
    Modifies multisetToModify so that its count for an element e is at most multisetToRetain.count(e).
    static <T,​ E,​ M extends Multiset<E>>
    java.util.stream.Collector<T,​?,​M>
    toMultiset​(java.util.function.Function<? super T,​E> elementFunction, java.util.function.ToIntFunction<? super T> countFunction, java.util.function.Supplier<M> multisetSupplier)
    Returns a Collector that accumulates elements into a multiset created via the specified Supplier, whose elements are the result of applying elementFunction to the inputs, with counts equal to the result of applying countFunction to the inputs.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • toMultiset

      public static <T,​ E,​ M extends Multiset<E>> java.util.stream.Collector<T,​?,​M> toMultiset​(java.util.function.Function<? super T,​E> elementFunction, java.util.function.ToIntFunction<? super T> countFunction, java.util.function.Supplier<M> multisetSupplier)
      Returns a Collector that accumulates elements into a multiset created via the specified Supplier, whose elements are the result of applying elementFunction to the inputs, with counts equal to the result of applying countFunction to the inputs. Elements are added in encounter order.

      If the mapped elements contain duplicates (according to Object.equals(java.lang.Object)), the element will be added more than once, with the count summed over all appearances of the element.

      Note that stream.collect(toMultiset(function, e -> 1, supplier)) is equivalent to stream.map(function).collect(Collectors.toCollection(supplier)).

      Since:
      22.0
    • containsOccurrences

      @CanIgnoreReturnValue public static boolean containsOccurrences​(Multiset<?> superMultiset, Multiset<?> subMultiset)
      Returns true if subMultiset.count(o) <= superMultiset.count(o) for all o.
      Since:
      10.0
    • retainOccurrences

      @CanIgnoreReturnValue public static boolean retainOccurrences​(Multiset<?> multisetToModify, Multiset<?> multisetToRetain)
      Modifies multisetToModify so that its count for an element e is at most multisetToRetain.count(e).

      To be precise, multisetToModify.count(e) is set to Math.min(multisetToModify.count(e), multisetToRetain.count(e)). This is similar to intersection (multisetToModify, multisetToRetain), but mutates multisetToModify instead of returning a view.

      In contrast, multisetToModify.retainAll(multisetToRetain) keeps all occurrences of elements that appear at all in multisetToRetain, and deletes all occurrences of all other elements.

      Returns:
      true if multisetToModify was changed as a result of this operation
      Since:
      10.0
    • removeOccurrences

      @CanIgnoreReturnValue public static boolean removeOccurrences​(Multiset<?> multisetToModify, java.lang.Iterable<?> occurrencesToRemove)
      For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.

      Equivalently, this method modifies multisetToModify so that multisetToModify.count(e) is set to Math.max(0, multisetToModify.count(e) - Iterables.frequency(occurrencesToRemove, e)).

      This is not the same as multisetToModify. removeAll(occurrencesToRemove), which removes all occurrences of elements that appear in occurrencesToRemove. However, this operation is equivalent to, albeit sometimes more efficient than, the following:

      
       for (E e : occurrencesToRemove) {
         multisetToModify.remove(e);
       }
       
      Returns:
      true if multisetToModify was changed as a result of this operation
      Since:
      18.0 (present in 10.0 with a requirement that the second parameter be a Multiset)
    • removeOccurrences

      @CanIgnoreReturnValue public static boolean removeOccurrences​(Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove)
      For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.

      Equivalently, this method modifies multisetToModify so that multisetToModify.count(e) is set to Math.max(0, multisetToModify.count(e) - occurrencesToRemove.count(e)).

      This is not the same as multisetToModify. removeAll(occurrencesToRemove), which removes all occurrences of elements that appear in occurrencesToRemove. However, this operation is equivalent to, albeit sometimes more efficient than, the following:

      
       for (E e : occurrencesToRemove) {
         multisetToModify.remove(e);
       }
       
      Returns:
      true if multisetToModify was changed as a result of this operation
      Since:
      10.0 (missing in 18.0 when only the overload taking an Iterable was present)