Kotlin Tutorials

Kotlin Set dropWhile()
Syntax & Examples

Set.dropWhile() extension function

The dropWhile() extension function for sets in Kotlin returns a list containing all elements of the set except the first elements that satisfy the given predicate.


Syntax of Set.dropWhile()

The syntax of Set.dropWhile() extension function is:

fun <T> Set<T>.dropWhile(predicate: (T) -> Boolean): List<T>

This dropWhile() extension function of Set returns a list containing all elements except the first elements that satisfy the given predicate.

Parameters

ParameterOptional/RequiredDescription
predicaterequiredA function that takes an element of the set and returns a Boolean indicating whether the element satisfies the condition.

Return Type

Set.dropWhile() returns value of type List.



✐ Examples

1 Using dropWhile() to drop elements less than 3 from a set

In Kotlin, we can use the dropWhile() function to get a list of elements from a set, excluding the first elements that are less than 3.

For example,

  1. Create a set of integers.
  2. Use the dropWhile() function with a predicate that checks if an element is less than 3.
  3. Print the resulting list to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val numbers = setOf(1, 2, 3, 4, 5)
    val droppedNumbers = numbers.dropWhile { it < 3 }
    println("Elements after dropping those less than 3: $droppedNumbers")
}

Output

Elements after dropping those less than 3: [3, 4, 5]

2 Using dropWhile() with a set of strings

In Kotlin, we can use the dropWhile() function to get a list of elements from a set of strings, excluding the first elements that start with a vowel.

For example,

  1. Create a set of strings.
  2. Use the dropWhile() function with a predicate that checks if the first character of the string is a vowel.
  3. Print the resulting list to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val fruits = setOf("apple", "banana", "apricot", "cherry")
    val droppedFruits = fruits.dropWhile { it.first() in "aeiou" }
    println("Elements after dropping those starting with a vowel: $droppedFruits")
}

Output

Elements after dropping those starting with a vowel: [banana, apricot, cherry]

3 Using dropWhile() with an empty set

In Kotlin, we can use the dropWhile() function on an empty set, which will return an empty list regardless of the predicate.

For example,

  1. Create an empty set of integers.
  2. Use the dropWhile() function with a predicate that checks if an element is positive.
  3. Print the resulting list to the console using the println function.

Kotlin Program

fun main(args: Array<String>) {
    val emptySet = emptySet<Int>()
    val droppedElements = emptySet.dropWhile { it > 0 }
    println("Elements after applying dropWhile on empty set: $droppedElements")
}

Output

Elements after applying dropWhile on empty set: []

Summary

In this Kotlin tutorial, we learned about dropWhile() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.