Kotlin – 28 – Sets in Kotlin

In Kotlin, a set is a collection that stores a unique set of elements, ensuring that each element appears only once. Sets are part of the Kotlin Standard Library and are used when you need to work with a collection of distinct values. In this article, we will explore sets in Kotlin, how to create them, perform operations, and work with common set functions.

Creating Sets

You can create sets in Kotlin using the setOf() function. This function initializes a read-only set with the provided elements. Here’s an example:


val fruits: Set<String> = setOf("apple", "banana", "cherry", "banana")

In this example, the fruits set contains four elements, but duplicates are automatically removed, leaving only unique values (“apple,” “banana,” and “cherry”).

Mutable Sets

If you need to create a mutable set that allows you to add or remove elements, you can use the mutableSetOf() function:


val mutableFruits: MutableSet<String> = mutableSetOf("apple", "banana", "cherry")
mutableFruits.add("date") // Adding an element
mutableFruits.remove("banana") // Removing an element

With mutable sets, you can modify the set’s contents as needed.

Set Operations

Sets in Kotlin support various operations and functions to work with their elements:

  • size: Returns the number of elements in the set.
  • isEmpty(): Checks if the set is empty.
  • contains(): Checks if the set contains a specific element.
  • add(): Adds an element to the set.
  • remove(): Removes an element from the set.
  • clear(): Removes all elements from the set.

Here’s an example demonstrating some of these operations:


val fruitsSize = fruits.size
val isEmpty = fruits.isEmpty()
val hasBanana = fruits.contains("banana")
mutableFruits.add("blueberry") // Adding an element
mutableFruits.remove("cherry") // Removing an element
mutableFruits.clear() // Removes all elements from the set

These operations allow you to manipulate and query sets effectively.

Iterating Over Sets

You can iterate over the elements of a set in Kotlin using a for loop or the forEach() function. Here’s a for loop example:


for (fruit in fruits) {
    println(fruit)
}

You can also use the forEach() function to perform actions on each element:


fruits.forEach { fruit -> println("Found $fruit") }
Set Operations and Mathematical Set Operations

Sets in Kotlin support mathematical set operations such as union, intersection, and difference. You can perform these operations using functions like union()intersect(), and subtract(). Here’s an example:


val setA: Set<Int> = setOf(1, 2, 3, 4, 5)
val setB: Set<Int> = setOf(4, 5, 6, 7, 8)

val unionSet = setA.union(setB) // Union of setA and setB
val intersectionSet = setA.intersect(setB) // Intersection of setA and setB
val differenceSet = setA.subtract(setB) // Difference between setA and setB

In this example, unionSet contains the elements from both setA and setBintersectionSet contains the common elements, and differenceSet contains the elements that are in setA but not in setB.

Conclusion

Sets in Kotlin are valuable data structures for working with collections of unique elements. Whether you need a read-only set or a mutable one, Kotlin provides convenient functions and operations for creating, modifying, and querying sets. Understanding how to work with sets and perform mathematical set operations can simplify many tasks in your Kotlin applications.