Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaFinding the Minimum or Maximum Value in Java ArrayList

Finding the Minimum or Maximum Value in Java ArrayList

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

  1. First, we need to initialize the ArrayList values.
  2. Then the length of the ArrayList can be found by using the size() function.
  3. After that, the first element of the ArrayList will be store in the variable min and max.
  4. 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);
    }
}


Output

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:

  1. First, we need to create a class.
  2. 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.
  3. The length of the ArrayList will be used to iterate through the loop and print the elements of the ArrayList.
  4. 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);
    }
}


Output

Array elements are :
10 20 5 8 
Maximum is : 20
Minimum is : 5

 Method 3: By sorting the ArrayList

  1. 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.
  2. After that, the ArrayList of integers will be created and then we will calculate the length using size() function.
  3. Then, the ArrayList will be sorted using the predefined function, and by default, it will be sorted in increasing order only.
  4. 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.
  5. 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.
  6. 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);
    }
}


Output

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
RELATED ARTICLES

Most Popular

Recent Comments