
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin List - groupBy() Function
The Kotlin List groupBy() function is used to group the element of the original list by the key returned by the given key selector (lambda) function applied to each element and returns a map where each group key is associated with the list of corresponding elements.
The returned map preserves the entry iteration order of the keys produced from the original collection. But this function does not manipulate the original collection.
Syntax
Following is the syntax of Kotlin list groupBy() function −
list.groupBy(keySelector: (T) -> K): Map<K, List<T>>
Parameters
This function accepts a keySelector as a parameter. A keySelector is a lambda function to define a logic to create a key.
Return value
This function returns a map where keys are selected by the key selector and values are lists of elements grouped by those keys.
Example 1: Group By First Letter
Let's see a basic example of the groupBy() function, which return a list that does not contains even number −
fun main(args: Array<String>) { val words = listOf("apple", "apricot", "banana", "blueberry", "cherry") val groupedByFirstLetter = words.groupBy { it.first() } println(groupedByFirstLetter) }
Output
Following is the output −
{a=[apple, apricot], b=[banana, blueberry], c=[cherry]}
Example 2: Grouping by String Length
In the following example, we use groupBy() to group the elements of a list based on their length −
fun main(args: Array<String>){ val words = listOf("Kotlin", "Java", "C++", "Python") val shortWords = words.groupBy{ it.length } println(shortWords) }
Output
Following is the output −
{6=[Kotlin, Python], 4=[Java], 3=[C++]}
Example 3: Group by Age
We create a class called Person that holds a name and age. Then, we use the groupBy() function to group the elements by age −
data class Employee(val name: String, val age: Int) fun main() { val people = listOf( Employee("Aman", 25), Employee("Rahul", 26), Employee("Dipak", 26), Employee("Akash", 25) ) val groupedByAge = people.groupBy { it.age } println(groupedByAge) }
Output
Following is the output −
{25=[Employee(name=Aman, age=25), Employee(name=Akash, age=25)], 26=[Employee(name=Rahul, age=26), Employee(name=Dipak, age=26)]}