Counter

A multiset that tracks how many times each element has been added.

Counts are always positive; removing an element down to zero deletes its key, and querying an absent element yields zero. Iteration and toMap follow first-insertion order. mostCommon sorts by count descending and breaks ties by first-insertion order. Backed by LinkedHashMap.

This class is not thread-safe.

Type Parameters

T

the element type.

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
val total: Int

The sum of the counts of all elements.

Functions

Link copied to clipboard
fun add(element: T, count: Int = 1)

Increases the count of element by count.

Link copied to clipboard

Returns true when every element equals the first one. An empty iterable returns true.

Link copied to clipboard

Returns true if at least one element occurs more than once.

Link copied to clipboard

Returns a map of each element to its index in this iterable.

Link copied to clipboard

Returns the average of all durations.

Link copied to clipboard
fun <T, R> Iterable<T>.cartesianProduct(other: Iterable<R>): List<Pair<T, R>>

Returns every pairing of an element of this iterable with an element of other.

Link copied to clipboard
inline fun <T, K> Iterable<T>.chunkedBy(selector: (T) -> K): List<List<T>>

Splits this iterable into consecutive runs, starting a new run whenever the value of selector changes.

Link copied to clipboard
fun clear()

Removes every element and resets total to zero.

Link copied to clipboard
fun count(element: T): Int

Returns the count of element, or 0 if it is absent.

Link copied to clipboard
inline fun <T, K> Iterable<T>.countBy(selector: (T) -> K): Map<K, Int>

Returns a map of each distinct value of selector to the number of elements producing it.

Link copied to clipboard
fun <T> Iterable<T>.duplicates(): Set<T>

Returns the set of elements that occur more than once.

Link copied to clipboard

Returns a map of each distinct element to the number of times it occurs.

Link copied to clipboard

Returns true if no element occurs more than once.

Link copied to clipboard
open operator override fun iterator(): Iterator<Map.Entry<T, Int>>

Returns an iterator over a snapshot of the entries in first-insertion order. Mutating the counter after this call does not affect the iterator.

Link copied to clipboard
inline fun <T, R> Iterable<T>.mapToSet(transform: (T) -> R): Set<R>

Returns a set of the results of applying transform to each element.

Link copied to clipboard

Returns the smallest and largest element as a pair, or null if the receiver is empty.

Link copied to clipboard
fun <T> Iterable<T>.mode(): T?

Returns the most frequent element, or null if the receiver is empty.

Link copied to clipboard
fun mostCommon(n: Int = Int.MAX_VALUE): List<Pair<T, Int>>

Returns up to n elements with their counts, highest count first.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelFilter(concurrency: Int = Int.MAX_VALUE, predicate: suspend (T) -> Boolean): List<T>

Evaluates predicate for every element concurrently and returns the matching elements in the original order.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelFilterNot(concurrency: Int = Int.MAX_VALUE, predicate: suspend (T) -> Boolean): List<T>

Evaluates predicate for every element concurrently and returns the non-matching elements in the original order.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelFlatMap(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> Iterable<R>): List<R>

Transforms every element concurrently and returns the flattened results in the original order.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelForEach(concurrency: Int = Int.MAX_VALUE, action: suspend (T) -> Unit)

Runs action for every element concurrently and returns when all have finished.

Link copied to clipboard
suspend fun <T> Iterable<T>.parallelForEachIndexed(concurrency: Int = Int.MAX_VALUE, action: suspend (index: Int, element: T) -> Unit)

Runs action for every element with its index concurrently and returns when all have finished.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelMap(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> R): List<R>

Transforms every element concurrently and returns the results in the original order.

Link copied to clipboard
suspend fun <T, R> Iterable<T>.parallelMapIndexed(concurrency: Int = Int.MAX_VALUE, transform: suspend (index: Int, element: T) -> R): List<R>

Transforms every element with its index concurrently and returns the results in the original order.

Link copied to clipboard
suspend fun <T, R : Any> Iterable<T>.parallelMapIndexedNotNull(concurrency: Int = Int.MAX_VALUE, transform: suspend (index: Int, element: T) -> R?): List<R>

Transforms every element with its index concurrently and returns the non-null results in the original order.

Link copied to clipboard
suspend fun <T, R : Any> Iterable<T>.parallelMapNotNull(concurrency: Int = Int.MAX_VALUE, transform: suspend (T) -> R?): List<R>

Transforms every element concurrently and returns the non-null results in the original order.

Link copied to clipboard
inline fun <T> Iterable<T>.partitionIndexed(predicate: (index: Int, T) -> Boolean): Pair<List<T>, List<T>>

Splits this iterable into a pair of lists using predicate, which also receives each element's index.

Link copied to clipboard
operator fun plusAssign(element: T)

Increases the count of element by one. Shorthand for add.

Link copied to clipboard
@JvmName(name = "productOfDouble")
fun Iterable<Double>.product(): Double
@JvmName(name = "productOfLong")
fun Iterable<Long>.product(): Long

Returns the product of all elements.

@JvmName(name = "productOfInt")
fun Iterable<Int>.product(): Long

Returns the product of all elements, accumulated as a Long.

Link copied to clipboard
fun remove(element: T, count: Int = 1)

Decreases the count of element by count, flooring at zero.

Link copied to clipboard
inline fun <T> Iterable<T>.splitBy(isDelimiter: (T) -> Boolean): List<List<T>>

Splits this iterable into segments separated by elements matching isDelimiter, dropping the delimiters.

Link copied to clipboard

Returns the sum of all durations, or Duration.ZERO for an empty iterable.

Link copied to clipboard
fun toMap(): Map<T, Int>

Returns a snapshot of the counts in first-insertion order.

Link copied to clipboard
inline fun <T> Iterable<AutoCloseable>.useAll(block: () -> T): T

Runs block and then closes every resource in this collection in reverse iteration order.