Kotlin Sets
Unique element collections without duplicates
🎯 What are Kotlin Sets?
Sets are collections that contain only unique elements with no duplicates allowed. They're ideal for storing distinct values, performing mathematical set operations, and ensuring data uniqueness in your applications efficiently.
// Creating a set with duplicates
val numbers = setOf(1, 2, 3, 2, 1, 4)
println("Set: $numbers") // Duplicates removed
// Check if element exists
println("Contains 3: ${3 in numbers}")
Output:
Set: [1, 2, 3, 4]
Contains 3: true
Set Characteristics
Unique Elements
No duplicate values allowed
setOf(1, 1, 2) // [1, 2]
Fast Lookups
Quick membership testing
element in set
Set Operations
Union, intersection, difference
set1 union set2
Mutable Sets
Add and remove elements
mutableSetOf()
🔹 Creating Sets
Different ways to create and initialize sets:
// Immutable set
val colors = setOf("Red", "Green", "Blue", "Red")
println("Colors: $colors") // Red appears only once
// Mutable set
val fruits = mutableSetOf("Apple", "Banana")
fruits.add("Orange")
fruits.add("Apple") // Won't add duplicate
// Empty sets
val emptySet = emptySet<String>()
val emptyMutable = mutableSetOf<Int>()
// From other collections
val listToSet = listOf(1, 2, 2, 3, 3, 4).toSet()
println("Fruits: $fruits")
println("From list: $listToSet")
println("Set size: ${colors.size}")
Output:
Colors: [Red, Green, Blue]
Fruits: [Apple, Banana, Orange]
From list: [1, 2, 3, 4]
Set size: 3
🔹 Set Operations
Mathematical operations between sets:
val setA = setOf(1, 2, 3, 4, 5)
val setB = setOf(4, 5, 6, 7, 8)
// Union - all elements from both sets
val union = setA union setB
val unionAlt = setA + setB
// Intersection - common elements
val intersection = setA intersect setB
// Difference - elements in A but not in B
val difference = setA - setB
// Symmetric difference - elements in either set but not both
val symmetricDiff = (setA union setB) - (setA intersect setB)
println("Set A: $setA")
println("Set B: $setB")
println("Union: $union")
println("Intersection: $intersection")
println("A - B: $difference")
println("Symmetric difference: $symmetricDiff")
Output:
Set A: [1, 2, 3, 4, 5]
Set B: [4, 5, 6, 7, 8]
Union: [1, 2, 3, 4, 5, 6, 7, 8]
Intersection: [4, 5]
A - B: [1, 2, 3]
Symmetric difference: [1, 2, 3, 6, 7, 8]
🔹 Working with Mutable Sets
Add, remove, and modify set contents:
val languages = mutableSetOf("Kotlin", "Java", "Python")
// Add elements
languages.add("JavaScript")
languages.addAll(listOf("Go", "Rust", "Python")) // Python won't be added twice
// Remove elements
languages.remove("Java")
val removed = languages.removeAll(listOf("Go", "C++")) // C++ not in set
// Check operations
val hasKotlin = languages.contains("Kotlin")
val hasAny = languages.any { it.startsWith("P") }
// Clear and check
val originalSize = languages.size
languages.clear()
val isEmpty = languages.isEmpty()
println("Languages: $languages")
println("Had Kotlin: $hasKotlin")
println("Had P language: $hasAny")
println("Original size: $originalSize")
println("Is empty now: $isEmpty")
Output:
Languages: []
Had Kotlin: true
Had P language: true
Original size: 5
Is empty now: true