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

Comments

Popular posts from this blog

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

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

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

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

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String