Operation Availability By Collection Type
β
- Available
β - Not Available
π - Returns different type
Operation | List | Set | Map | Array | String | Sequence |
---|
size | β
| β
| β
| β
| β
(length ) | β |
isEmpty() | β
| β
| β
| β
| β
| β
|
contains() | β
| β
| β | β
| β
| β
|
containsKey() | β | β | β
| β | β | β |
indexOf() | β
| β | β | β
| β
| β |
get(index) | β
| β | β | β
| β
| β |
get(key) | β | β | β
| β | β | β |
Operation | List | Set | Map | Array | Sequence |
---|
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
Operation | List | Set | Map | Array | Sequence |
---|
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
- Return Types:
- Most operations on Set/Map return List
- Sequence operations stay lazy until terminal operation
- 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
- 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