Kotlin collection methods – groupBy(), sumByDouble(), mapValues(), toSortedMap() example

kotlin-collection-methods-groupby-sumbydouble-mapvalues-tosortedmap-feature-images-new

In the tutorial, Grokonez will show you how to combine Kotlin collection methods: groupBy(), sumByDouble() with Kotlin List and mapValues(), toSortedMap() with Kotlin Map.

1. Kotlin collection methods – groupBy(), sumByDouble(), mapValues(), toSortedMap()

groupBy() with method signature:


public inline fun  Iterable.groupBy(keySelector: (T) -> K): Map>

-> groupBy() is used to group elements of the given collection by the key returned by the given [keySelector] function. Result is a map collection.

Practice:


val productList = listOf(Product("iPhone 8 Plus 64G", "Apple", 850.00),
							Product("Samsung Galaxy S8 64GB Unlocked Phone", "Samsung", 699.99),
							Product("iPad Pro 9.7-inch 32 GB", "Apple", 474.98),
							Product("Samsung Electronics Ultra HD Smart LED TV", "Samsung", 677.92),
							Product("Google Pixel Phone - 5 inch display", "Google", 279.95),
							Product("iPad Pro 9.7-inch 128G", "Apple", 574.99),
							Product("Google Wifi system 1-Pack", "Google", 149.90),
							Product("Samsung Galaxy Tab 4", "Samsung", 127.67))

val groupProductsWithMadeBy = productList.groupBy { it-> it.madeBy }
groupProductsWithMadeBy.forEach { println(it) }
/*
	Apple=[Product(name=iPhone 8 Plus 64G, madeBy=Apple, price=850.0), Product(name=iPad Pro 9.7-inch 32 GB, madeBy=Apple, price=474.98), Product(name=iPad Pro 9.7-inch 128G, madeBy=Apple, price=574.99)]
	Samsung=[Product(name=Samsung Galaxy S8 64GB Unlocked Phone, madeBy=Samsung, price=699.99), Product(name=Samsung Electronics Ultra HD Smart LED TV, madeBy=Samsung, price=677.92), Product(name=Samsung Galaxy Tab 4, madeBy=Samsung, price=127.67)]
	Google=[Product(name=Google Pixel Phone - 5 inch display, madeBy=Google, price=279.95), Product(name=Google Wifi system 1-Pack, madeBy=Google, price=149.9)]
*/

mapValues() with method signature:


public inline fun  Map.mapValues(transform: (Map.Entry) -> R): Map

-> mapValues() is used to create a new map that uses [transform] function to produce the new values.

And sumByDouble() with method signature:


public inline fun  Iterable.sumByDouble(selector: (T) -> Double): Double

-> sumByDouble() returns the sum of all values produced by [selector] function applied to each element in the collection..

Practice:


val sumProductsPriceMap = groupProductsWithMadeBy.mapValues {(company, prods) -> prods.sumByDouble{ it -> it.price }}
sumProductsPriceMap.forEach{ println(it) }
/*
	Apple=1899.97
	Samsung=1505.58
	Google=429.85
*/

toSortedMap() with method signature:


public fun , V> Map.toSortedMap(): SortedMap

-> toSortedMap() converts this [Map] to a [SortedMap]

Practice:


val sortProductPriceMap = sumProductsPriceMap.toSortedMap()
sortProductPriceMap.forEach{ println(it) }
/*
	Apple=1899.97
	Google=429.85
	Samsung=1505.58
*/

2. Full Sourcecode



package com.javasampleapproach.kotlingroup

data class Product(
	val name: String,
	val madeBy: String,
	val price: Double // Usd
)

fun main(args : Array) {
	val productList = listOf(Product("iPhone 8 Plus 64G", "Apple", 850.00),
								Product("Samsung Galaxy S8 64GB Unlocked Phone", "Samsung", 699.99),
								Product("iPad Pro 9.7-inch 32 GB", "Apple", 474.98),
								Product("Samsung Electronics Ultra HD Smart LED TV", "Samsung", 677.92),
								Product("Google Pixel Phone - 5 inch display", "Google", 279.95),
								Product("iPad Pro 9.7-inch 128G", "Apple", 574.99),
								Product("Google Wifi system 1-Pack", "Google", 149.90),
								Product("Samsung Galaxy Tab 4", "Samsung", 127.67))
	
	println("--------------------Map with list products: Entry--------------------")
	// Use signature method:	
	// -  public inline fun  Iterable.groupBy(keySelector: (T) -> K): Map>
	val groupProductsWithMadeBy = productList.groupBy { it-> it.madeBy }
	groupProductsWithMadeBy.forEach { println(it) }
	/*
		Apple=[Product(name=iPhone 8 Plus 64G, madeBy=Apple, price=850.0), Product(name=iPad Pro 9.7-inch 32 GB, madeBy=Apple, price=474.98), Product(name=iPad Pro 9.7-inch 128G, madeBy=Apple, price=574.99)]
		Samsung=[Product(name=Samsung Galaxy S8 64GB Unlocked Phone, madeBy=Samsung, price=699.99), Product(name=Samsung Electronics Ultra HD Smart LED TV, madeBy=Samsung, price=677.92), Product(name=Samsung Galaxy Tab 4, madeBy=Samsung, price=127.67)]
		Google=[Product(name=Google Pixel Phone - 5 inch display, madeBy=Google, price=279.95), Product(name=Google Wifi system 1-Pack, madeBy=Google, price=149.9)]
	*/
	
	println("--------------------Map with sum price: Entry--------------------")
	// Use to signature methods:
	// - public inline fun  Map.mapValues(transform: (Map.Entry) -> R): Map
	// - public inline fun  Iterable.sumByDouble(selector: (T) -> Double): Double
	val sumProductsPriceMap = groupProductsWithMadeBy.mapValues {(company, prods) -> prods.sumByDouble{ it -> it.price }}
	sumProductsPriceMap.forEach{ println(it) }
	/*
		Apple=1899.97
		Samsung=1505.58
		Google=429.85
 	*/
	
	println("--------------------Sorted map with sum price: Entry--------------------")
	// Use signature method:
	// - public fun , V> Map.toSortedMap(): SortedMap
	val sortProductPriceMap = sumProductsPriceMap.toSortedMap()
	sortProductPriceMap.forEach{ println(it) }
	/*
		Apple=1899.97
		Google=429.85
		Samsung=1505.58
	*/
}

3 thoughts on “Kotlin collection methods – groupBy(), sumByDouble(), mapValues(), toSortedMap() example”

  1. I got this site from my buddy who told me regarding this site and at the moment this time I am visiting
    this website and reading very informative articles or
    reviews at this time.

  2. Hi there! This is my first comment here so I just wanted to give a quick shout out and
    say I truly enjoy reading your posts. Can you suggest any other blogs/websites/forums that deal with the same topics?
    Appreciate it!

  3. I am really loving the theme/design of your weblog.

    Do you ever run into any internet browser compatibility problems?
    A number of my blog visitors have complained about my blog not working correctly in Explorer but looks
    great in Opera. Do you have any tips to help fix this problem?

Leave a Reply

Your email address will not be published. Required fields are marked *