largest number in list java code example
Example 1: java how to find the largest number in an arraylist
List<Integer> myList = new ArrayList<>();
for(int i = 0; i < 10; i++){
myList.add(i);
}
int highestNumber = Collections.max(myList);
System.out.Println(highestNumber);
Example 2: how to get the highest value in a list java
import java.util.*;
public class CollectionsMaxExample2 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(20, 10, 100, 140, 250);
Integer max = Collections.max(list);
System.out.println("Maximum element is: "+max);
}