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:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts