public static int max(List<Integer> numbers) { int max = Integer.MIN_VALUE; for (Integer n: numbers) { if (n > max) { max = n; } } return max; }
example:
/** * @author lautturi.com * Java example: find Max value in java list */ import java.util.*; public class Lautturi { public static int max(List<Integer> numbers) { int max = Integer.MIN_VALUE; for (Integer n: numbers) { if (n > max) { max = n; } } return max; } public static void main(String[] args) { List<Integer> numbers = List.of( 1,2,3,4,5,6,7,8,9,10 ); int maxvaule = max(numbers); System.out.println("max:"+maxvaule); } }
output:
max:10