MutableMultiset

A generic unordered collection that supports order-independent equality, like Set, but may have duplicate elements. A multiset is also sometimes called a bag.

See the Guava User Guide article on Multiset.

See also

Parameters

E

the type of elements contained in the set. The set is covariant in its element type.

Functions

Link copied to clipboard
abstract fun add(element: E): Boolean

abstract fun add(element: E, occurrences: Int): Int

Adds a number of occurrences of an element to this multiset.

Link copied to clipboard
abstract fun addAll(elements: Collection<E>): Boolean
Link copied to clipboard
abstract fun clear()
Link copied to clipboard
abstract operator fun contains(element: E): Boolean
Link copied to clipboard
abstract fun containsAll(elements: Collection<E>): Boolean
Link copied to clipboard
abstract fun count(element: E): Int

Returns the number of occurrences of an element in this multiset (the count of the element). Note that for an Object.equals-based multiset, this gives the same result as Collections.frequency (which would presumably perform more poorly).

Link copied to clipboard
open fun forEach(p0: Consumer<in E>)
Link copied to clipboard
abstract override fun isEmpty(): Boolean

Returns true if the multiset is empty (contains no elements), false otherwise.

Link copied to clipboard
abstract operator override fun iterator(): Iterator<E>
Link copied to clipboard
open fun parallelStream(): Stream<E>
Link copied to clipboard
abstract fun remove(element: E): Boolean

abstract fun remove(element: E, occurrences: Int): Int

Removes a number of occurrences of the specified element from this multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed.

Link copied to clipboard
abstract fun removeAll(elements: Collection<E>): Boolean
Link copied to clipboard
open fun removeIf(p0: Predicate<in E>): Boolean
Link copied to clipboard
abstract fun retainAll(elements: Collection<E>): Boolean
Link copied to clipboard
abstract fun setCount(element: E, count: Int): Int

Adds or removes the necessary occurrences of an element such that the element attains the desired count.

abstract fun setCount(element: E, oldCount: Int, newCount: Int): Boolean

Conditionally sets the count of an element to a new value, as described in setCount, provided that the element has the expected current count. If the current count is not oldCount, no change is made.

Link copied to clipboard
open override fun spliterator(): Spliterator<E>
Link copied to clipboard
open fun stream(): Stream<E>
Link copied to clipboard
open fun <T : Any> toArray(p0: IntFunction<Array<T>>): Array<T>

Properties

Link copied to clipboard
abstract val elementSet: Set<E>

Returns a read-only Set of all non-duplicate values in this set.

Link copied to clipboard
abstract val entrySet: Set<Multiset.Entry<E>>

Returns a read-only Collection of all elements in this set, grouped into Multiset.Entry instances, each providing an element of the multiset and the count of that element.

Link copied to clipboard
abstract override val size: Int

Returns the number of elements in the multiset.

Extensions

Link copied to clipboard
inline fun <T> MutableCollection<T>.addAll(elementsToAdd: Iterable<T>): Boolean

Adds all elements in this to collection.

inline fun <T> MutableCollection<T>.addAll(iterator: Iterator<T>): Boolean

Adds all elements in iterator to this. The iterator will be left exhausted: its hasNext method will return false.

Link copied to clipboard
inline fun <T> Iterable<T>.all(noinline predicate: (T) -> Boolean): Boolean

Returns true if every element in this satisfies the predicate. If this is empty, true is returned.

Link copied to clipboard
inline fun <T> Iterable<T>.any(noinline predicate: (T) -> Boolean): Boolean

Returns true if any element in this satisfies the predicate.

Link copied to clipboard
inline fun <T> Iterable<T>.asString(): String

Returns a string representation of this, with the format [e1, e2, ..., en] (that is, identical to Arrays.toString(Iterables.toArray(iterable))). Note that for most implementations of Collection, collection.toString() also gives the same result, but that behavior is not generally guaranteed.

Link copied to clipboard
infix inline fun <T> Iterable<T>.concat(other: Iterable<T>): Iterable<T>
@JvmName(name = "concatMutable")
infix inline fun <T> MutableIterable<T>.concat(other: MutableIterable<T>): MutableIterable<T>

Combines two iterables into a single iterable. The returned iterable has an iterator that traverses the elements in this, followed by the elements in other. The source iterators are not polled until necessary.

inline fun <T> Iterable<T>.concat(a: Iterable<T>, b: Iterable<T>): Iterable<T>
@JvmName(name = "concatMutable")
inline fun <T> MutableIterable<T>.concat(a: MutableIterable<T>, b: MutableIterable<T>): MutableIterable<T>

Combines three iterables into a single iterable. The returned iterable has an iterator that traverses the elements in this, followed by the elements in a, followed by the elements in b. The source iterators are not polled until necessary.

inline fun <T> Iterable<T>.concat(a: Iterable<T>, b: Iterable<T>, c: Iterable<T>): Iterable<T>
@JvmName(name = "concatMutable")
inline fun <T> MutableIterable<T>.concat(a: MutableIterable<T>, b: MutableIterable<T>, c: MutableIterable<T>): MutableIterable<T>

Combines four iterables into a single iterable. The returned iterable has an iterator that traverses the elements in this, followed by the elements in a, followed by the elements in b, followed by the elements in c. The source iterators are not polled until necessary.

inline fun <T> Iterable<T>.concat(vararg iterables: Iterable<T>): Iterable<T>
@JvmName(name = "concatMutable")
inline fun <T> MutableIterable<T>.concat(vararg iterables: MutableIterable<T>): MutableIterable<T>

Combines multiple iterables into a single iterable. The returned iterable has an iterator that traverses the elements in this, followed by the elements of each iterable in iterables sequentially. The source iterators are not polled until necessary.

Link copied to clipboard

Returns a view of the supplied iterable that wraps each generated Iterator through Iterators.consumingIterator.

Link copied to clipboard
inline fun <T> Iterable<T>.cycleIterable(): Iterable<T>
@JvmName(name = "cycleIterableMutable")
inline fun <T> MutableIterable<T>.cycleIterable(): MutableIterable<T>

Returns an iterable whose iterators cycle indefinitely over the elements of this.

Link copied to clipboard
inline fun <T> Iterable<T>.cycleIterator(): Iterator<T>
@JvmName(name = "cycleMutable")
inline fun <T> MutableIterable<T>.cycleIterator(): MutableIterator<T>

Returns an iterator that cycles indefinitely over the elements of this.

Link copied to clipboard
inline fun <T> Iterable<T>.elementsEqual(other: Iterable<T>): Boolean

Determines whether two iterables contain equal elements in the same order. More specifically, this method returns true if this and other contain the same number of elements and every element of other is equal to the corresponding element of other.

Link copied to clipboard

Returns a view of this containing all elements that are of the type T.

Link copied to clipboard
inline fun <E> Collection<E>.filterView(noinline predicate: (E) -> Boolean): Collection<E>
@JvmName(name = "filterMutable")
inline fun <E> MutableCollection<E>.filterView(noinline predicate: (E) -> Boolean): MutableCollection<E>

Returns the elements of this that satisfy a predicate. The returned collection is a live view of this; changes to one affect the other.

inline fun <T> Iterable<T>.filterView(noinline retainIfTrue: (T) -> Boolean): Iterable<T>

Returns a view of this containing all elements that satisfy the input predicate retainIfTrue.

Link copied to clipboard
inline fun <T> Iterable<T>.find(defaultValue: T? = null, noinline predicate: (T) -> Boolean): T

Returns the first element in this that satisfies the given predicate; use this method only when such an element is known to exist. If it is possible that no element will match, use tryFind or find instead.

Link copied to clipboard
inline fun <T> Iterable<Iterable<T>>.flatConcat(): Iterable<T>
@JvmName(name = "flatConcatMutable")
inline fun <T> Iterable<MutableIterable<T>>.flatConcat(): MutableIterable<T>

Combines multiple iterables into a single iterable. The returned iterable has an iterator that traverses the elements in this sequentially. The source iterators are not polled until necessary.

inline fun <T> Iterable<Iterator<T>>.flatConcat(): Iterator<T>
@JvmName(name = "flatConcatMutable")
inline fun <T> Iterable<MutableIterator<T>>.flatConcat(): MutableIterator<T>

Combines two iterators into a single iterator. The returned iterator iterates across the elements in this sequentially. The source iterators are not polled until necessary.

Link copied to clipboard
inline fun <T> Iterable<T>.frequency(element: T): Int

Returns the number of elements in the specified iterable that equal the specified object. This implementation avoids a full iteration when the iterable is a Multiset or Set.

Link copied to clipboard
inline operator fun <T> Iterable<T>.get(position: Int): T

Returns the element at the specified position in an iterable.

inline fun <T> Iterable<T>.get(position: Int, defaultValue: T? = null): T

Returns the element at the specified position in an iterable or a default value otherwise.

Link copied to clipboard
inline fun <T> Iterable<T>.getFirst(defaultValue: T): T

Returns the first element in this or defaultValue if the iterable is empty. The Iterators analog to this method is Iterators.getNext.

Link copied to clipboard
inline fun <T> Iterable<T>.getLast(defaultValue: T? = null): T

Returns the last element of this or defaultValue if the iterable is empty. If this is a List with RandomAccess support, then this operation is guaranteed to be O(1).

Link copied to clipboard
inline fun <T> Iterable<T>.indexOf(noinline predicate: (T) -> Boolean): Int

Returns the index in this of the first element that satisfies the provided predicate, or -1 if the Iterable has no such elements.

Link copied to clipboard
inline fun <T> Iterable<T>.isEmpty(): Boolean

Determines if the given iterable contains no elements.

Link copied to clipboard
inline fun <T> Iterable<T>.limit(limitSize: Int): Iterable<T>

Returns a view of this containing its first limitSize elements. If this contains fewer than limitSize elements, the returned view contains all of its elements. The returned iterable's iterator supports remove() if this's iterator does.

Link copied to clipboard
inline fun <T, R : Comparable<R>> Iterable<Iterable<T>>.mergeSorted(crossinline selector: (T) -> Comparable<R>): Iterable<T>

Returns an iterable over the merged contents of all given Iterables in this. Equivalent entries will not be de-duplicated.

inline fun <T, R : Comparable<R>> Iterable<Iterator<T>>.mergeSorted(crossinline selector: (T) -> Comparable<R>): Iterator<T>

Returns an iterator over the merged contents of all given this, traversing every element of the input iterators. Equivalent entries will not be de-duplicated.

Link copied to clipboard

Returns an unmodifiable view of the difference of two multisets. In the returned multiset, the count of each element is the result of the zero-truncated subtraction of its count in the second multiset from its count in the first multiset, with elements that would have a count of 0 not included. The iteration order of the returned multiset matches that of the element set of this, with repeated occurrences of the same element appearing consecutively.

Link copied to clipboard

Returns a Collection of all the permutations of the specified Iterable using naturalOrder for establishing the lexicographical ordering.

Returns a Collection of all the permutations of the specified Iterable using the specified selector for establishing the lexicographical ordering.

Returns a Collection of all the permutations of the specified Iterable using the specified comparator for establishing the lexicographical ordering.

Link copied to clipboard
inline fun <T> Iterable<T>.paddedPartition(size: Int): Iterable<List<T?>>

Divides an iterable into unmodifiable sublists of the given size, padding the final iterable with null values if necessary. For example, partitioning an iterable containing [a, b, c, d, e] with a partition size of 3 yields [[a, b, c], [d, e, null]] -- an outer iterable containing two inner lists of three elements each, all in the original order.

Link copied to clipboard
inline fun <T> Iterable<T>.partition(size: Int): Iterable<List<T>>

Divides an iterable into unmodifiable sublists of the given size (the final iterable may be smaller). For example, partitioning an iterable containing [a, b, c, d, e] with a partition size of 3 yields [[a, b, c], [d, e]] -- an outer iterable containing two inner lists of three and two elements, all in the original order.

Link copied to clipboard

Returns a Collection of all the permutations of the specified Collection.

Link copied to clipboard

Returns an unmodifiable view of the sum of two multisets. In the returned multiset, the count of each element is the sum of its counts in the two backing multisets. The iteration order of the returned multiset matches that of the element set of this followed by the members of the element set of other that are not contained in other, with repeated occurrences of the same element appearing consecutively.

Link copied to clipboard
inline fun <T> MutableIterable<T>.removeAll(elementsToRemove: Collection<T>): Boolean

Removes, from an iterable, every element that belongs to the provided collection.

Link copied to clipboard
inline fun <T> MutableIterable<T>.removeIf(noinline predicate: (T) -> Boolean): Boolean

Removes, from an iterable, every element that satisfies the provided predicate.

Link copied to clipboard
inline fun <T> MutableIterable<T>.retainAll(elementsToRetain: Collection<T>): Boolean

Removes, from an iterable, every element that does not belong to the provided collection.

Link copied to clipboard
val Iterable<*>.size: Int

Returns the number of elements in this.

Link copied to clipboard
inline fun <T> Iterable<T>.skip(numberToSkip: Int): Iterable<T>
@JvmName(name = "skipMutable")
inline fun <T> MutableIterable<T>.skip(numberToSkip: Int): Iterable<T>

Returns a view of this that skips its first numberToSkip elements. If this contains fewer than numberToSkip elements, the returned iterable skips all of its elements.

Link copied to clipboard

Transforms a MutableMultiset into its guava equivalent

Transforms a Multiset into its immutable guava equivalent

Link copied to clipboard

Returns a new Multiset filled with all elements of this collection.

Link copied to clipboard

Returns a new MutableMultiset filled with all elements of this collection.

Link copied to clipboard
inline fun <T> Iterable<T>.toTypedArray(): Array<out T>

Copies an iterable's elements into an array.

Link copied to clipboard
inline fun <F, T> Collection<F>.transform(noinline function: (F) -> T): Collection<T>
@JvmName(name = "transformMutable")
inline fun <F, T> MutableCollection<F>.transform(noinline function: (F) -> T): MutableCollection<T>

Returns a collection that applies function to each element of this. The returned collection is a live view of this; changes to the base collection affects the returned one.

inline fun <F, T> Iterable<F>.transform(noinline function: (F) -> T): Iterable<T>

Returns a view containing the result of applying function to each element of this.

@JvmName(name = "transformMutable")
inline fun <F, T> MutableIterable<F>.transform(noinline function: (F) -> T): MutableIterable<T>

Returns a view containing the result of applying function to each element of fromIterable.

Link copied to clipboard
inline fun <T> Iterable<T>.tryFind(noinline predicate: (T) -> Boolean): Optional<T>

Returns an Optional containing the first element in this that satisfies the given predicate, if such an element exists.

Link copied to clipboard

Returns an unmodifiable view of this.