Post

Collection Operations By Type in Kotlin

Collection Operations By Type in Kotlin

Operation Availability By Collection Type

βœ… - Available
❌ - Not Available
πŸ”„ - Returns different type

OperationListSetMapArrayStringSequence
sizeβœ…βœ…βœ…βœ…βœ… (length)❌
isEmpty()βœ…βœ…βœ…βœ…βœ…βœ…
contains()βœ…βœ…βŒβœ…βœ…βœ…
containsKey()βŒβŒβœ…βŒβŒβŒ
indexOf()βœ…βŒβŒβœ…βœ…βŒ
get(index)βœ…βŒβŒβœ…βœ…βŒ
get(key)βŒβŒβœ…βŒβŒβŒ

Transformation Operations

OperationListSetMapArraySequence
filterβœ…πŸ”„ListπŸ”„ListπŸ”„Listβœ…
mapβœ…πŸ”„ListπŸ”„ListπŸ”„Listβœ…
flattenβœ…πŸ”„ListβŒπŸ”„Listβœ…
flatMapβœ…πŸ”„ListπŸ”„ListπŸ”„Listβœ…
sortedβœ…πŸ”„ListβŒπŸ”„ListπŸ”„List
distinctβœ…πŸ”„ListβŒπŸ”„Listβœ…
groupByβœ…βœ…βœ…βœ…βœ…
associateβœ…βœ…βœ…βœ…βœ…

Aggregate Operations

OperationListSetMapArraySequence
countβœ…βœ…βœ…βœ…βœ…
sumβœ…βœ…βŒβœ…βœ…
averageβœ…βœ…βŒβœ…βœ…
max/minβœ…βœ…βŒβœ…βœ…
reduceβœ…βœ…βœ…βœ…βœ…
foldβœ…βœ…βœ…βœ…βœ…

Examples By Collection Type

List Operations

1
2
3
4
5
6
val list = listOf(1, 2, 3, 4)
list.get(0)              // 1
list[0]                  // 1
list.indexOf(2)          // 1
list.subList(1, 3)       // [2, 3]
list.sorted()            // [1, 2, 3, 4]

Set Operations

1
2
3
4
5
val set = setOf(1, 2, 3)
set.contains(1)          // true
// set[0]               // ❌ No index access
set.sorted()            // Returns List [1, 2, 3]
set.filter { it > 1 }   // Returns List [2, 3]

Map Operations

1
2
3
4
5
6
7
val map = mapOf("a" to 1, "b" to 2)
map["a"]                // 1
map.get("a")            // 1
map.getOrDefault("c", 0) // 0
map.containsKey("a")    // true
map.keys               // Set of keys
map.values            // Collection of values

Array Operations

1
2
3
4
5
val array = arrayOf(1, 2, 3)
array[0]               // 1
array.get(0)           // 1
array.indexOf(2)       // 1
array.sorted()         // Returns List [1, 2, 3]

Sequence Operations

1
2
3
4
val seq = sequenceOf(1, 2, 3)
// seq[0]             // ❌ No index access
seq.filter { it > 1 } // Returns Sequence
seq.toList()          // Converts to List

Converting Between Types

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// To List
setOf(1, 2, 3).toList()
mapOf("a" to 1).toList()
arrayOf(1, 2, 3).toList()
sequenceOf(1, 2, 3).toList()

// To Set
listOf(1, 2, 3).toSet()
mapOf("a" to 1).toSet()
arrayOf(1, 2, 3).toSet()
sequenceOf(1, 2, 3).toSet()

// To Map
listOf("a" to 1).toMap()
setOf("a" to 1).toMap()
arrayOf("a" to 1).toMap()

// To Array
listOf(1, 2, 3).toTypedArray()
setOf(1, 2, 3).toTypedArray()

Important Notes

  1. Return Types:
    • Most operations on Set/Map return List
    • Sequence operations stay lazy until terminal operation
  2. Performance Implications:
    • Set: O(1) for contains
    • List: O(n) for contains, O(1) for index access
    • Map: O(1) for key access
    • Sequence: Lazy evaluation, good for large collections
  3. Mutability: ```kotlin // Mutable Collections have additional operations val mutableList = mutableListOf(1, 2, 3) mutableList.add(4) mutableList.removeAt(0)

val mutableSet = mutableSetOf(1, 2, 3) mutableSet.add(4) mutableSet.remove(1)

val mutableMap = mutableMapOf(β€œa” to 1) mutableMap[β€œb”] = 2 mutableMap.remove(β€œa”) ```

Remember:

  • Always choose the most appropriate collection type for your use case
  • Consider using Sequences for large collections with multiple operations
  • When performance is critical, understand the time complexity of operations
This post is licensed under CC BY 4.0 by the author.