In this tutorial, we will see “How to find Smallest and Largest Numbers in an Array using Java 8?”
Find Smallest and Largest number in an array in java 8 using streams
Java 8 – How to find the ‘Smallest’ and ‘Largest’ numbers in an array using Streams? Share on X
/**
* Using Java 8, Find the Smallest and Largest number in an array
* @author Deepak Verma
*
*/
import java.util.Arrays;
public class Find_Smallest_Largest_Number_In_Array_Java8Stream_Example {
public static void main(String args[]) {
int[] numbers = {25, 99, 321, 4, 73, 666, 432, 18, 53};
int smallestNumber = Arrays.stream(numbers)
.min()
.getAsInt();
int largestNumber = Arrays.stream(numbers)
.max()
.getAsInt();
System.out.println("Smallest Number in the Array: " + smallestNumber);
System.out.println("Largest Number in the Array: " + largestNumber);
}
}
Output:
Smallest Number in the Array: 4 Largest Number in the Array: 666
Find Smallest and Largest number in an array in java 8 using streams
Java 8 – How to find the ‘Smallest’ and ‘Largest’ numbers in an array using Streams? Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: