Introduction
Sets in Kotlin are unordered collections of unique elements.
Unlike lists, sets do not allow duplicate elements.
Kotlin provides two types of sets: Set
for immutable sets and MutableSet
for mutable sets.
This chapter will cover how to create and manipulate both immutable and mutable sets, along with common operations that can be performed on sets.
Immutable Sets
An immutable set cannot be modified after it is created. You can use the setOf
function to create an immutable set.
Creating an Immutable Set
Syntax
val set: Set<Type> = setOf(elements)
Example
fun main() {
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
println(fruits)
}
Explanation:
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
: Creates an immutable set of strings.
Output:
[Apple, Banana, Cherry]
Mutable Sets
A mutable set can be modified after it is created. You can use the mutableSetOf
function to create a mutable set.
Creating a Mutable Set
Syntax
val set: MutableSet<Type> = mutableSetOf(elements)
Example
fun main() {
val fruits: MutableSet<String> = mutableSetOf("Apple", "Banana")
fruits.add("Cherry")
println(fruits)
}
Explanation:
val fruits: MutableSet<String> = mutableSetOf("Apple", "Banana")
: Creates a mutable set of strings.fruits.add("Cherry")
: Adds an element to the mutable set.
Output:
[Apple, Banana, Cherry]
Common Set Operations
Kotlin provides various operations that can be performed on sets, such as checking membership, iterating through sets, adding/removing elements, and more.
Checking Membership
You can check if an element is in a set using the contains
function or the in
operator.
Example
fun main() {
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
println("Apple" in fruits) // Prints "true"
println(fruits.contains("Banana")) // Prints "true"
println(fruits.contains("Mango")) // Prints "false"
}
Output:
true
true
false
Iterating Through a Set
You can iterate through a set using a for
loop.
Example
fun main() {
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
for (fruit in fruits) {
println(fruit)
}
}
Output:
Apple
Banana
Cherry
Adding and Removing Elements (Mutable Set)
You can add and remove elements from a mutable set using various methods.
Example
fun main() {
val fruits: MutableSet<String> = mutableSetOf("Apple", "Banana")
fruits.add("Cherry")
println(fruits) // Prints "[Apple, Banana, Cherry]"
fruits.remove("Banana")
println(fruits) // Prints "[Apple, Cherry]"
}
Output:
[Apple, Banana, Cherry]
[Apple, Cherry]
Union, Intersection, and Difference
Kotlin provides functions to perform union, intersection, and difference operations on sets.
Example
fun main() {
val set1: Set<Int> = setOf(1, 2, 3)
val set2: Set<Int> = setOf(3, 4, 5)
val union = set1.union(set2)
println("Union: $union") // Prints "[1, 2, 3, 4, 5]"
val intersection = set1.intersect(set2)
println("Intersection: $intersection") // Prints "[3]"
val difference = set1.subtract(set2)
println("Difference: $difference") // Prints "[1, 2]"
}
Output:
Union: [1, 2, 3, 4, 5]
Intersection: [3]
Difference: [1, 2]
Converting Set to List
You can convert a set to a list using the toList
function.
Example
fun main() {
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
val fruitList: List<String> = fruits.toList()
println(fruitList)
}
Output:
[Apple, Banana, Cherry]
Example Program with Sets
Here is an example program that demonstrates various aspects of using sets in Kotlin:
fun main() {
// Immutable set
val fruits: Set<String> = setOf("Apple", "Banana", "Cherry")
println(fruits)
// Mutable set
val mutableFruits: MutableSet<String> = mutableSetOf("Apple", "Banana")
mutableFruits.add("Cherry")
println(mutableFruits)
// Checking membership
println("Apple" in fruits)
println(fruits.contains("Banana"))
println(fruits.contains("Mango"))
// Iterating through a set
for (fruit in fruits) {
println(fruit)
}
// Adding and removing elements (mutable set)
mutableFruits.remove("Banana")
println(mutableFruits)
// Union, intersection, and difference
val set1: Set<Int> = setOf(1, 2, 3)
val set2: Set<Int> = setOf(3, 4, 5)
val union = set1.union(set2)
println("Union: $union")
val intersection = set1.intersect(set2)
println("Intersection: $intersection")
val difference = set1.subtract(set2)
println("Difference: $difference")
// Converting set to list
val fruitList: List<String> = fruits.toList()
println(fruitList)
}
Output:
[Apple, Banana, Cherry]
[Apple, Banana, Cherry]
true
true
false
Apple
Banana
Cherry
[Apple, Cherry]
Union: [1, 2, 3, 4, 5]
Intersection: [3]
Difference: [1, 2]
[Apple, Banana, Cherry]
Conclusion
In this chapter, you learned about sets in Kotlin, including how to create and manipulate immutable and mutable sets. You also learned about common set operations such as checking membership, iterating through sets, adding/removing elements, and performing union, intersection, and difference operations.