0
0
Kotlinprogramming~10 mins

Set creation (setOf, mutableSetOf) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Set creation (setOf, mutableSetOf)
Call setOf or mutableSetOf
Create empty or filled Set
Add elements (if mutableSetOf)
Return Set object
Use Set in code
This flow shows how calling setOf or mutableSetOf creates a Set, optionally adds elements if mutable, and returns it for use.
Execution Sample
Kotlin
val fruits = setOf("apple", "banana", "apple")
val mutableFruits = mutableSetOf("apple", "banana")
mutableFruits.add("cherry")
println(fruits)
println(mutableFruits)
Creates an immutable set with duplicates ignored and a mutable set where elements can be added.
Execution Table
StepActionSet State (fruits)Set State (mutableFruits)Output
1Call setOf("apple", "banana", "apple"){}{}
2Create immutable set with unique elements[apple, banana]{}
3Call mutableSetOf("apple", "banana")[apple, banana]{}
4Create mutable set with elements[apple, banana][apple, banana]
5Add "cherry" to mutableFruits[apple, banana][apple, banana, cherry]
6Print fruits[apple, banana][apple, banana, cherry][apple, banana]
7Print mutableFruits[apple, banana][apple, banana, cherry][apple, banana, cherry]
💡 All steps complete; sets created and printed; duplicates ignored in immutable set.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
fruitsempty[apple, banana][apple, banana][apple, banana][apple, banana]
mutableFruitsemptyempty[apple, banana][apple, banana, cherry][apple, banana, cherry]
Key Moments - 2 Insights
Why does the immutable set 'fruits' not contain duplicate "apple"?
Because sets only keep unique elements, the duplicate "apple" is ignored when creating 'fruits' as shown in step 2 of the execution_table.
Can we add elements to 'fruits' after creation?
'fruits' is created with setOf which returns an immutable set, so you cannot add elements after creation. Only 'mutableFruits' created with mutableSetOf allows adding elements (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the state of 'mutableFruits' after adding "cherry"?
A[apple, banana, cherry]
B[apple, banana]
C[cherry]
Dempty
💡 Hint
Check the 'Set State (mutableFruits)' column at step 5 in the execution_table.
At which step does the 'fruits' set get created with unique elements?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Set State (fruits)' column in the execution_table to see when it changes from empty to containing elements.
If we tried to add an element to 'fruits', what would happen?
AIt would add the element successfully.
BIt would silently ignore the addition.
CIt would cause a compile-time error.
DIt would remove an existing element.
💡 Hint
Recall that 'fruits' is created with setOf which returns an immutable set, so adding elements is not allowed.
Concept Snapshot
setOf(...) creates an immutable set with unique elements.
mutableSetOf(...) creates a mutable set where you can add or remove elements.
Duplicates are ignored in sets.
Use add() to add elements only on mutable sets.
Immutable sets cannot be changed after creation.
Full Transcript
This example shows how to create sets in Kotlin using setOf and mutableSetOf. The setOf function creates an immutable set that automatically removes duplicates, so repeated elements like "apple" appear only once. The mutableSetOf function creates a set that you can change later by adding elements. In the example, we create 'fruits' as an immutable set with "apple" and "banana". Then we create 'mutableFruits' as a mutable set with the same elements and add "cherry" to it. Printing both sets shows that 'fruits' has two elements and 'mutableFruits' has three. Trying to add elements to 'fruits' would cause an error because it is immutable. This teaches how sets store unique items and the difference between immutable and mutable sets in Kotlin.