Kotlin: Print Odd & Even Numbers from Array, List & Set

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)
}
}

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete