Write a Java program to Find maximum element in ArrayList


This Java code creates an ArrayList of Strings called num_list and adds five elements to it. It then uses the Collections.max() method to find the maximum value in the list, which is returned as an Object reference called max_num. Finally, it prints out the maximum value found in the list using the System.out.println() method.

Note that the elements added to num_list are actually Strings, so the max() method will return the maximum String based on the lexicographic order, not the maximum number. If the intention is to find the maximum number, the elements should be added as integers instead of Strings.

Source Code

import java.util.ArrayList;
import java.util.Collections;
public class Find_Maximum
{
	public static void main(String[] args)
	{
		ArrayList num_list = new ArrayList();
		num_list.add("80");
		num_list.add("18");
		num_list.add("48");
		num_list.add("37");
		num_list.add("24");
		Object max_num = Collections.max(num_list);
		System.out.println("Maximum Element ArrayList is : " + max_num);
	}
}

Output

Maximum Element ArrayList is : 80

Example Programs