Blog Detail

  • Home
  • Find Max value from Array of numbers

Find Max value from Array of numbers

Sample program for find the maximum value from array of numbers in java. by using stream and for loop.

package com.samplecoder.streams;

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

public class FindMax {
	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().max((num1,num2) -> num1.compareTo(num2)).get();
		System.out.println("MAX 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("MAX Value - Without Stream API " + minNum);
	}
}

Result:

MAX Value - With Stream API 545
MAX Value - Without Stream API 545

Write a comment