To get a sub Set in Kotlin, you can use the slice function with the range of indices.
In this example,
mySet
containing elements of a specific type.slice
function with the specified range to get the sub Set.fun main() {
// Define Set with items
val mySet = setOf("apple", "banana", "orange", "grape", "melon")
// Get sub Set with items
val subSet = mySet.slice(1..3)
println("Sub Set with items: $subSet")
}
Sub Set with items: [banana, orange, grape]
In this example,
mySet
.fun main() {
// Define empty Set
val mySet = emptySet<String>()
// Get sub Set from empty Set
val subSet = mySet.slice(1..3)
println("Sub Set from empty Set: $subSet")
}
Sub Set from empty Set: []
In this tutorial, we learned How to get Subset from a Set in Kotlin language with well detailed examples.