W3cubDocs

/Kotlin

Grouping

interface Grouping<T, out K>

Platform and version requirements: Kotlin 1.1

Represents a source of elements with a keyOf function, which can be applied to each element to get its key.

A Grouping structure serves as an intermediate step in group-and-fold operations: they group elements by their keys and then fold each group with some aggregating operation.

It is created by attaching keySelector: (T) -> K function to a source of elements. To get an instance of Grouping use one of groupingBy extension functions:

For the list of group-and-fold operations available, see the extension functions for Grouping.

Functions

keyOf

abstract fun keyOf(element: T): K

Extracts the key of an element.

sourceIterator

abstract fun sourceIterator(): Iterator<T>

Returns an Iterator over the elements of the source of this grouping.

Inherited Functions

equals

open operator fun equals(other: Any?): Boolean

Indicates whether some other object is "equal to" this one. Implementations must fulfil the following requirements:

hashCode

open fun hashCode(): Int

Returns a hash code value for the object. The general contract of hashCode is:

toString

open fun toString(): String

Returns a string representation of the object.

Extension Functions

aggregate

fun <T, K, R> Grouping<T, K>.aggregate(
    operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
): Map<K, R>

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in a new map.

aggregateTo

fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.aggregateTo(
    destination: M, 
    operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
): M

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in the given destination map.

eachCount

fun <T, K> Grouping<T, K>.eachCount(): Map<K, Int>

Groups elements from the Grouping source by key and counts elements in each group.

eachCountTo

fun <T, K, M : MutableMap<in K, Int>> Grouping<T, K>.eachCountTo(
    destination: M
): M

Groups elements from the Grouping source by key and counts elements in each group to the given destination map.

fold

fun <T, K, R> Grouping<T, K>.fold(
    initialValueSelector: (key: K, element: T) -> R, 
    operation: (key: K, accumulator: R, element: T) -> R
): Map<K, R>

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in a new map. An initial value of accumulator is provided by initialValueSelector function.

fun <T, K, R> Grouping<T, K>.fold(
    initialValue: R, 
    operation: (accumulator: R, element: T) -> R
): Map<K, R>

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in a new map. An initial value of accumulator is the same initialValue for each group.

foldTo

fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
    destination: M, 
    initialValueSelector: (key: K, element: T) -> R, 
    operation: (key: K, accumulator: R, element: T) -> R
): M

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in the given destination map. An initial value of accumulator is provided by initialValueSelector function.

fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
    destination: M, 
    initialValue: R, 
    operation: (accumulator: R, element: T) -> R
): M

Groups elements from the Grouping source by key and applies operation to the elements of each group sequentially, passing the previously accumulated value and the current element as arguments, and stores the results in the given destination map. An initial value of accumulator is the same initialValue for each group.

reduce

fun <S, T : S, K> Grouping<T, K>.reduce(
    operation: (key: K, accumulator: S, element: T) -> S
): Map<K, S>

Groups elements from the Grouping source by key and applies the reducing operation to the elements of each group sequentially starting from the second element of the group, passing the previously accumulated value and the current element as arguments, and stores the results in a new map. An initial value of accumulator is the first element of the group.

reduceTo

fun <S, T : S, K, M : MutableMap<in K, S>> Grouping<T, K>.reduceTo(
    destination: M, 
    operation: (key: K, accumulator: S, element: T) -> S
): M

Groups elements from the Grouping source by key and applies the reducing operation to the elements of each group sequentially starting from the second element of the group, passing the previously accumulated value and the current element as arguments, and stores the results in the given destination map. An initial value of accumulator is the first element of the group.

© 2010–2018 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-grouping/index.html