Skip to content
Related Articles

Related Articles

Improve Article

Kotlin Aggregate operations

  • Last Updated : 17 Sep, 2019

Aggregate operation is an operation that is performed on a data structure, such as an array, as a whole rather than performed on an individual element.

    Functions that used aggregate operations are as follows:

  1. count() function is used to returns the number of elements.
  2. sum() function is used to returns the sum of elements in the collection of numbers.
  3. min() functions are used to return the smallest element.
  4. max() functions are used to return the largest element.
  5. average() function is used to returns the average value of elements in the collection of numbers.
  6. maxBy() function returns a Collector that produces the maximal element according to a given .
  7. minBy() function returns a Collector that produces the minimal element according to a given .
  8. maxWith() function take a Comparator object and generally return the largest element.
  9. minWith() function take a Comparator object and generally return the smallest element.
  10. sumBy() performs an efficient and optionally weighted by-group summation by using linear algebra and the Matrix package capabilities..
  11. sumByDouble() Returns the sum of all values produced by selector function applied to each element in the array..

Kotlin program of using some of aggregate functions –




//Functions that used aggregate operations
fun main(args: Array<String>) {
    val numbers = listOf(6,34,57,78,12,4,5,5)
  
    println("Count the number of element in the list: ${numbers.count()}")
    println("Sum of the elements in the list: ${numbers.sum()}")
    println("Min value in the list: ${numbers.min()}")
    println("Max value in the list: ${numbers.max()}")
    println("Average of all elements in the list: ${numbers.average()}")
}

Output:

Count the number of element in the list: 8
Sum of the elements in the list: 201
Min value in the list: 4
Max value in the list: 78
Average of all elements in the list: 25.125

Kotlin program of using sumBy() and sumByDouble() functions-






//Functions that used aggregate operations
fun main(args: Array<String>) {
    val numbers = listOf(6,34,57,78,12,4,5,5)
    println("Multiply each element with 3 and sum: "+numbers.sumBy { it * 3 })
    println(numbers.sumByDouble { it.toDouble() / 2 })
}

Output:

Multiply each element with 3 and sum: 603
100.5

Kotlin program of using minBy() and maxWith() functions –




//Functions that used aggregate operations
fun main(args: Array<String>) {
    val numbers = listOf(6,34,57,78,12,4,5,5)
    val minRem = numbers.minBy { it % 4}
    println("Minimum remainder returned by: " +minRem)
  
    val strings = listOf("Cricket","Football","Volley","Chess")
    val maxstrln = strings.maxWith(compareBy { it.length })
    println("String with max length is: " +maxstrln)
}

Output:

Minimum remainder returned by: 12
String with max length is: Football

Fold and Reduce Functions

  • fold() function: It takes an associative binary operator function as parameter and will use it to collapse elements from the collection. .
  • foldRight() function :It work in a way similar to fold(). The order for traversing the elements in the collection is from right to left.
  • foldIndexed() function : It is used to get access of the current index while iterating.
  • foldRightIndexed() function: It work in a way similar to foldIndexed(). The order for traversing the elements in the collection is from right to left
  • reduce() function reduces the array to a single value. It executes a provided function for each value of the array.
  • reduceRight() function :It work in a way similar to reduce(). The order for removing of the elements in the collection is from right to left.
  • reduceIndexed() function accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element with its index in the given collection.
  • reduceRightIndexed() function: It work in a way similar to reduceIndexed(). The order in the collection is from right to left.

Kotlin program of using fold() and reduce() functions –




//Fold and Reduce Functions
fun main(args: Array<String>) {
    val numbers = listOf(57,78,12,4,5,5, 42)
    val sum = numbers.reduce { sum, element -> sum + element }
    println("The sum of all elements in list "+sum)
    val sumThrice = numbers.fold(0) { sum, element -> sum + element * 3}
    println("Multiply each element with 3 and sum "+sumThrice)
}

Output:

The sum of all elements in list 203
Multiply each element with 3 and sum 609

Kotlin program of using foldRight() function –




//Fold and Reduce Functions
fun main(args: Array<String>) {
    val numbers = listOf(6,34,57,78,12,4,5,5, 4)
    val sumThriceRight= numbers.foldRight(0){element, sum -> sum + element*3}
    println("The sum of the numbers from the right side: "+sumThriceRight)
}

Output:

The sum of the numbers from the right side: 615

Kotlin program of using foldIndexed() and foldRightIndexed() –




//Fold and Reduce Functions
fun main(args: Array<String>) {
    val numbers = listOf(6,34,57,78,12,4,5,5, 42, 10, 4)
    val sumOfEvenNumbers = numbers.foldIndexed(0)
    { idx, sum, element -> if (idx % 2 == 0) sum + element else sum }
    println("Sum of even numbers: "+sumOfEvenNumbers)
  
    val sumOfEvenFromRight = numbers.foldRightIndexed(0)
    { idx, element, sum -> if (idx % 2 == 0) sum + element else sum }
    println("Sum of even numbers from Right side: "+sumOfEvenFromRight)
}

Output:

Sum of even numbers: 126
Sum of even numbers from Right side: 126



My Personal Notes arrow_drop_up
Recommended Articles
Page :