import java.util.*; public class MaxExample { public static void main(String[] args) { Listlist = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50); int max = Collections.max(list); System.out.println("The maximum number is: " + max); } }
The maximum number is: 50
import java.util.*; public class MaxExample { public static void main(String[] args) { Listlist = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("mango"); list.add("pineapple"); list.add("watermelon"); String max = Collections.max(list, Comparator.comparing(String::length)); System.out.println("The maximum length string is: " + max); } }
The maximum length string is: watermelonThis method is available in the java.util.Collections package.