To create a set of strings in Kotlin, you can use the mutableSetOf function or the setOf function.
In this example,
mutableSetOf
function to create a mutable set of strings.add
method.fun main() {
// Create a mutable set of strings
val stringSet = mutableSetOf<String>()
// Add strings to the set
stringSet.add("Apple")
stringSet.add("Banana")
stringSet.add("Cherry")
// Print the set
println(stringSet)
}
[Apple, Banana, Cherry]
In this example,
setOf
function to create an immutable set of strings.fun main() {
// Create an immutable set of strings
val stringSet = setOf("Apple", "Banana", "Cherry")
// Print the set
println(stringSet)
}
[Apple, Banana, Cherry]
In this tutorial, we learned How to create a Set of Strings in Kotlin language with well detailed examples.