Blog Detail

  • Home
  • Find min number from Array of numbers

Find min number from Array of numbers

package com.samplecoder.streams;

import java.util.Arrays;
import java.util.List;

public class FindMin {
	public static void main(String... args) {
		List<Integer> numbers = Arrays.asList(100,3,200,2,1,23,43,545,232);
		
		// With Stream API
		int min = numbers.stream().min((num1,num2) -> num1.compareTo(num2)).get();
		System.out.println("MIN Value - With Stream API "+min);
		
		//Without steam API
		int minNum = numbers.get(0);
		for(int i=0; i<numbers.size(); i++) {
			if(minNum > numbers.get(i))
				minNum = numbers.get(i);
		}
		
		System.out.println("MIN Value - Without Stream API " + minNum);
	}
}
Output:

MIN Value - With Stream API 1
MIN Value - Without Stream API 1

Find minimum value from the given collection of numbers in Java 8 by using Stream API and without Stream API.

Write a comment