Finding Maximum Element of Java ArrayList

11 May, 2021
For finding the maximum element in the ArrayList, complete traversal of the ArrayList is required. There is an inbuilt function in the ArrayList class to find the maximum element in the ArrayList, i.e. Time Complexity is O(N), where N is the size of ArrayList, Let’s discuss both the methods.
Example:
Input : ArrayList = {2, 9, 1, 3, 4}
Output: Max = 9

Input : ArrayList = {6, 7, 2, 1}
Output: Max = 7
Approach 1:
Create on variable and initialize it with the first element of ArrayList.Start traversing the ArrayList.If the current element is greater than variable, then update the variable with the current element in ArrayList.In the end, print that variable.
Create on variable and initialize it with the first element of ArrayList.
Start traversing the ArrayList.
If the current element is greater than variable, then update the variable with the current element in ArrayList.
In the end, print that variable.
Below is the implementation of the above approach:
Java
// Finding Maximum Element of Java ArrayListimport java.util.ArrayList;import java.util.Collections; class MinElementInArrayList {     public static void main(String[] args)    {        // ArrayList of Numbers        ArrayList<Integer> myList            = new ArrayList<Integer>();         // adding elements to Java ArrayList        myList.add(16);        myList.add(26);        myList.add(3);        myList.add(52);        myList.add(70);        myList.add(12);         int maximum = myList.get(0);        for (int i = 1; i < myList.size(); i++) {            if (maximum < myList.get(i))                maximum = myList.get(i);        }        System.out.println("Maximum Element in ArrayList = "                           + maximum);    }}
Maximum Element in ArrayList = 70

Approach 2:
The max method of the Java collection class can be used to find ArrayList. The max method returns the maximum element of the collection according to the natural ordering of the elements.
Java
// Finding Maximum Element of Java ArrayListimport java.util.ArrayList;import java.util.Collections; class MinElementInArrayList {     public static void main(String[] args)    {         // ArrayList of Numbers        ArrayList<Integer> myList            = new ArrayList<Integer>();         // adding elements to Java ArrayList        myList.add(16);        myList.add(26);        myList.add(3);        myList.add(52);        myList.add(70);        myList.add(12);         // 'min' method is used to find the        // minimum elementfrom Collections Class        System.out.println("Maximum Element in ArrayList = "                           + Collections.max(myList));    }}
Maximum Element in ArrayList = 70
shekharsaxena316
Java-ArrayList
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array