Example 1: Kotlin program to print odd numbers from a List
import java.util.stream.Collectors
/*Kotlin Program to find Odd Numbers from a list*/
object DriverClass {
@JvmStatic
fun main(args: Array<String>) {
val numbers = listOf(1, 4, 8, 40, 11, 22, 33, 99)
val oddNumbers = numbers.stream().
filter { o: Int -> o % 2 != 0 }.
collect(Collectors.toList())
println(oddNumbers)
}
}
Example 2: Kotlin program to print even numbers from a List
import java.util.stream.Collectors
/*Kotlin Program to find Even Numbers from a list*/
object DriverClass {
@JvmStatic
fun main(args: Array<String>) {
val numbers = listOf(1, 4, 8, 40, 11, 22, 33, 99)
val evenNumbers = numbers.stream().
filter { o: Int -> o % 2 == 0 }.
collect(Collectors.toList())
println(evenNumbers)
}
}
Example 3: Kotlin program to print even numbers from an array
import java.util.*
/*Kotlin Program to find Even Numbers from an Array*/
object DriverClass {
@JvmStatic
fun main(args: Array<String>) {
val numbers = intArrayOf(2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66)
Arrays.stream(numbers).
filter { o: Int -> o % 2 == 0 }.
forEach { x: Int -> println(x) }
}
}
Example 4: Kotlin program to print odd numbers from an array
import java.util.*
/*Kotlin Program to find Odd Numbers from an Array*/
object DriverClass {
@JvmStatic
fun main(args: Array<String>) {
val numbers = intArrayOf(2, 5, 7, 8, 99, 1, 22, 4, 3, 77, 66)
Arrays.stream(numbers).
filter { o: Int -> o % 2 != 0 }.
forEach { x: Int -> println(x) }
}
}
Example 5: Kotlin program to print odd numbers from a Set
import java.util.stream.Collectors
/*Kotlin Program to find Odd Numbers from a set*/
object DriverClass {
@JvmStatic
fun main(args: Array<String>) {
val numbers = setOf(1, 4, 8, 40, 11, 22, 33, 99)
val oddNumbers = numbers.stream().
filter { o: Int -> o % 2 != 0 }.
collect(Collectors.toSet())
println(oddNumbers)
}
}
Example 6: Kotlin program to print even numbers from a Set
import java.util.stream.Collectors
/*Java 8 Program to find Odd Numbers from a set*/
object DriverClass {
@JvmStatic
fun main(args: Array<String>) {
val numbers = setOf(1, 4, 8, 40, 11, 22, 33, 99)
val evenNumbers = numbers.stream().
filter { o: Int -> o % 2 == 0 }.
collect(Collectors.toSet())
println(evenNumbers)
}
}