Finding Maximum Element of Java ArrayList Last Updated : 11 May, 2021 Comments Improve Suggest changes Like Article Like Report 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 = 7Approach 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.Below is the implementation of the above approach: Java // Finding Maximum Element of Java ArrayList import 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); } } OutputMaximum 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 ArrayList import 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)); } } OutputMaximum Element in ArrayList = 70 Comment More infoAdvertise with us Next Article Finding Maximum Element of Java ArrayList shekharsaxena316 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-ArrayList +1 More Practice Tags : Java Similar Reads Remove first element from ArrayList in Java Given an ArrayList collection in Java, the task is to remove the first element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 40, 50] Output: [20, 30, 40, 50]Input: ArrayList[] = [1, 1, 2, 2, 3]Output: [1, 2, 2, 3]We can use the remove() method of ArrayList container in Java to remov 2 min read How to Replace a Element in Java ArrayList? To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the 2 min read How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class. 2 min read How to Add an Element at Particular Index in Java ArrayList? ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Ex 2 min read Get Enumeration Over Java ArrayList ArrayList class is a re-sizeable array that is present in java.util package. Unlike the built-in arrays, ArrayList can change their size dynamically where elements can be added and removed from an ArrayList. In java version 1, enum was not present. With advancement to version, 1.5 enum was introduce 3 min read Comparing two ArrayList In Java Java provides a method for comparing two Array List. The ArrayList.equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Example: Input : ArrayLis 2 min read Sort and Search an Element in Java In Java sorting and searching an element in an array is very easy. Unlike C, where we have to make all the functions to work, Java has inbuilt functions to do the same work. To sort an array there is a sort function and to search an element in a sorted array there is a binarySearch() function. To le 3 min read Convert HashSet to a ArrayList in Java ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can b 4 min read How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value 3 min read Like