Kotlin Set filter Example

Filtering is an operation where only elements that meet certain criteria pass through.

Kotlin Set filter Example

The following example presents the filtering operation on Kotlin sets.
package net.sourcecodeexamples.kotlin

fun main(args: Array<String>) {

 val fruits = setOf("banana", "mango", "apple", "orange")

 val fruits1 = fruits.filter { e -> e.length == 5 }
 fruits1.forEach { e -> print("$e ") }

 println()

 val fruits2 = fruits.filterNot { e -> e.length == 5 }

 fruits2.forEach { e -> print("$e ") }
 println()
}
Output:
mango apple 
banana orange 



Comments