Finding the Minimum or Maximum Value in Java ArrayList - GeeksforGeeks

15 Dec, 2020
The minimum value is the one with the smallest value and the maximum value is the one with the largest value. The main task here is to find the minimum and maximum value from the ArrayList. Consider an example of an ArrayList, and we need to find the largest and the smallest element. 
Example:
Input List: {10, 20, 8, 32, 21, 31};

Output: 

Maximum is: 32

Minimum is: 8
Method 1: By iterating over ArrayList values
First, we need to initialize the ArrayList values.Then the length of the ArrayList can be found by using the size() function.After that, the first element of the ArrayList will be store in the variable min and max.Then the for loop is used to iterate through the ArrayList elements one by one in order to find the minimum and maximum from the array list.
First, we need to initialize the ArrayList values.
Then the length of the ArrayList can be found by using the size() function.
After that, the first element of the ArrayList will be store in the variable min and max.
Then the for loop is used to iterate through the ArrayList elements one by one in order to find the minimum and maximum from the array list.
Java
// Java program to find the minimum and// maximum value of the ArrayList  import java.util.*;public class Max {    public static void main(String args[])    {          // initializing the ArrayList elements        ArrayList<Integer> arr = new ArrayList<>();        arr.add(10);        arr.add(20);        arr.add(8);        arr.add(32);        arr.add(21);        arr.add(31);          int min = arr.get(0);        int max = arr.get(0);          // store the length of the ArrayList in variable n        int n = arr.size();          // loop to find minimum from ArrayList        for (int i = 1; i < n; i++) {            if (arr.get(i) < min) {                min = arr.get(i);            }        }          // loop to find maximum from ArrayList        for (int i = 1; i < n; i++) {            if (arr.get(i) > max) {                max = arr.get(i);            }        }          // The result will be printed        System.out.println("Maximum is : " + max);        System.out.println("Minimum is : " + min);    }}
Maximum is : 32
Minimum is : 8
Method 2: Using Collection class Methods
We can use the min() and max() method of the collection class of Java. Collections in java is basically a framework that provides an architecture to accumulate and handle the group of objects. Java Collection framework provides many classes such as ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet.
Approach:
First, we need to create a class.Then an integer ArrayList needs to be created to store the elements. After that, the length of the ArrayList should be calculated with the help of the size() method.The length of the ArrayList will be used to iterate through the loop and print the elements of the ArrayList.Then, the min and max method of collection class will be used to find the minimum and maximum from the ArrayList and will store in the min and max variable and then the result will be printed on the screen.
First, we need to create a class.
Then an integer ArrayList needs to be created to store the elements. After that, the length of the ArrayList should be calculated with the help of the size() method.
The length of the ArrayList will be used to iterate through the loop and print the elements of the ArrayList.
Then, the min and max method of collection class will be used to find the minimum and maximum from the ArrayList and will store in the min and max variable and then the result will be printed on the screen.
Java
// Java program to find the minimum and// maximum element of the list using// Collection class's method  import java.util.ArrayList;import java.util.Collections;  public class MinMax {    public static void main(String args[])    {        // Creating arraylist        ArrayList<Integer> arr = new ArrayList<Integer>();          // Adding object in arraylist        arr.add(10);        arr.add(20);        arr.add(5);        arr.add(8);          // Finding the size of ArrayList        int n = arr.size();          // printing the ArrayList elements        System.out.println("ArrayList elements are :");          for (int i = 0; i < n; i++) {            System.out.print(arr.get(i) + " ");        }          System.out.println();          // Finding the minimum and maximum from the        // arraylist using min and max method of collection        // class          int max = Collections.max(arr);        System.out.println("Maximum is : " + max);          int min = Collections.min(arr);        System.out.println("Minimum is : " + min);    }}
Array elements are :
10 20 5 8 
Maximum is : 20
Minimum is : 5
 Method 3: By sorting the ArrayList
First, we need to import the Collections class, because in the Collections class there is a method called Collections.sort() which we need to sort the unsorted array.After that, the ArrayList of integers will be created and then we will calculate the length using size() function.Then, the ArrayList will be sorted using the predefined function, and by default, it will be sorted in increasing order only.For finding minimum and maximum values from the ArrayList, we simply need to find the first and last element of the ArrayList, because the ArrayList is sorted in ascending order then the first element will be the smallest and the last element will be largest among all of the elements.The first element can be found by using arr.get(0), because it is present in the first position and the index of the array is started from 0.The last element can be found by using arr.get(n-1), since n is the size of the array and array index is started from 0, that’s why we need to find the element that is in index n-1. Also, this is a sorted ArrayList then the largest element is present at the end.
First, we need to import the Collections class, because in the Collections class there is a method called Collections.sort() which we need to sort the unsorted array.
After that, the ArrayList of integers will be created and then we will calculate the length using size() function.
Then, the ArrayList will be sorted using the predefined function, and by default, it will be sorted in increasing order only.
For finding minimum and maximum values from the ArrayList, we simply need to find the first and last element of the ArrayList, because the ArrayList is sorted in ascending order then the first element will be the smallest and the last element will be largest among all of the elements.
The first element can be found by using arr.get(0), because it is present in the first position and the index of the array is started from 0.
The last element can be found by using arr.get(n-1), since n is the size of the array and array index is started from 0, that’s why we need to find the element that is in index n-1. Also, this is a sorted ArrayList then the largest element is present at the end.
Java
// Java program to get the minimum and// maximum value after the sorting the ArrayList// using Collection class function  import java.util.*;import java.util.Collections;  public class MaxMinSort {    public static void main(String args[])    {        // Creating arraylist        ArrayList<Integer> arr = new ArrayList<Integer>();          // Adding object in arraylist        arr.add(10);        arr.add(12);        arr.add(5);        arr.add(8);        arr.add(21);        arr.add(16);        arr.add(15);          // Storing the size of the ArrayList in variable n        int n = arr.size();        int i;          System.out.println("Elements of the ArrayList : ");          for (i = 0; i < n; i++) {            System.out.print(arr.get(i) + " ");        }          System.out.println();          // The ArrayList will be sorted using predefined        // function        Collections.sort(arr);          System.out.println("ArrayList after sorting : ");          for (i = 0; i < n; i++) {            System.out.print(arr.get(i) + " ");        }          System.out.println();          // First and last element of the ArrayList gets stored        // into min and max variable        int min = arr.get(0);        int max = arr.get(n - 1);          System.out.println("Maximum is : " + max);        System.out.println("Minimum is : " + min);    }}
Elements of the array : 
10 12 5 8 21 16 15 
Arrays after sorting : 
5 8 10 12 15 16 21 
Maximum is : 21
Minimum is : 5
Java-ArrayList
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Generics in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?