Kotlin - Finding largest & smallest element in an Array, List & Set

Example 1: Finding the largest element in an Array

import java.util.*

/*
Kotlin Program to find the largest number in an Array.
*/
object Demo {
@JvmStatic
fun main(args: Array<String>) {
val numbers = intArrayOf(2, 6, 7, 9, 5, 155, 66, 99)
val largestNumber = Arrays.stream(numbers).max().asInt
println("Largest Number= $largestNumber")
}
}
Output:

Largest Number= 155


Example 2: Finding the largest element in a List

import java.util.*

/*
Kotlin Program to find the largest number in a List.
*/
object Demo {
@JvmStatic
fun main(args: Array<String>) {
val numbers = listOf(2, 6, 7, 9, 5, 155, 66, 99)
val largestNumber = numbers.stream().max(Comparator.
comparing { i: Int? ->
Integer.valueOf(i!!) }).get()
println("Largest Number= $largestNumber")
}
}
Output:

Largest Number= 155


Example 3: Finding the largest element in a Set

import java.util.Set

/*
Kotlin Program to find the largest number in a Set.
*/
object Demo {
@JvmStatic
fun main(args: Array<String>) {
val numbers = Set.of(2, 6, 7, 9, 5, 155, 66, 99)
val largestNumber = numbers.stream().max(Comparator.comparing
{ i: Int? -> Integer.valueOf(i!!) }).
get()
println("Largest Number= $largestNumber")
}
}
Output:

Largest Number= 155


Example 4: Finding the smallest element in a Set

import java.util.Set

/*
Kotlin Program to find the smallest number in a Set
*/
object Demo {
@JvmStatic
fun main(args: Array<String>) {
val numbers = setOf(2, 6, 7, 9, 5, 55, 4, 99)
val smallestNumber = numbers.stream().
min(Comparator.comparing
{ i: Int? -> Integer.
valueOf(i!!) }).get()
println("Smallest Number= $smallestNumber")
}
}
Output:

Largest Number= 2


Example 5: Finding the smallest element in a List

import java.util.*

/*
Kotlin Program to find the smallest number in a List
*/
object Demo {
@JvmStatic
fun main(args: Array<String>) {
val numbers = listOf(2, 6, 7, 9, 5, 55, 4, 11)
val smallestNumber = numbers.stream().
min(Comparator.comparing
{
i: Int? -> Integer.valueOf(i!!)
}).get()
println("Smallest Number= $smallestNumber")
}
}
Output:

Largest Number= 2


Example 6: Finding the smallest element in an Array

import java.util.*

/*
Kotlin Program to find the smallest number in an array
*/
object Demo {
@JvmStatic
fun main(args: Array<String>) {
val numbers = intArrayOf(12, 6, 7, 9, 5, 55, 4, 99)
val smallestNumber = Arrays.stream(numbers).min().asInt
println("Smallest Number= $smallestNumber")
}
}
Output:

Largest Number= 4

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