The drop() extension function for sets in Kotlin returns a list containing all elements of the set except the first n elements.
The syntax of Set.drop() extension function is:
fun <T> Set<T>.drop(n: Int): List<T>
This drop() extension function of Set returns a list containing all elements except the first n elements.
Parameter | Optional/Required | Description |
---|---|---|
n | required | The number of elements to drop from the beginning of the set. |
Set.drop() returns value of type List
.
In Kotlin, we can use the drop()
function to get a list of elements from a set, excluding the first 2 elements.
For example,
drop()
function to skip the first 2 elements.println
function.fun main(args: Array<String>) {
val numbers = setOf(1, 2, 3, 4, 5)
val droppedNumbers = numbers.drop(2)
println("Elements after dropping the first 2: $droppedNumbers")
}
Elements after dropping the first 2: [3, 4, 5]
In Kotlin, we can use the drop()
function to get a list of elements from a set of strings, excluding the first 3 elements.
For example,
drop()
function to skip the first 3 elements.println
function.fun main(args: Array<String>) {
val fruits = setOf("apple", "banana", "cherry", "date", "elderberry")
val droppedFruits = fruits.drop(3)
println("Elements after dropping the first 3: $droppedFruits")
}
Elements after dropping the first 3: [date, elderberry]
In Kotlin, we can use the drop()
function on an empty set, which will return an empty list regardless of the number of elements to drop.
For example,
drop()
function to skip the first 2 elements.println
function.fun main(args: Array<String>) {
val emptySet = emptySet<Int>()
val droppedElements = emptySet.drop(2)
println("Elements after dropping the first 2 in empty set: $droppedElements")
}
Elements after dropping the first 2 in empty set: []
In this Kotlin tutorial, we learned about drop() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.