Finding the Minimum or Maximum Value in Java ArrayList
Last Updated :
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.
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);
}
}
OutputMaximum 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.
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);
}
}
OutputArray 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.
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);
}
}
OutputElements 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
Similar Reads
How to Find the Minimum or Maximum Element from the Vector in Java? The Collection is a framework offered by Java that provides an architecture to store a group of objects. One such collection is Vector(). There are many ways through which we can find the minimum and maximum elements in a Vector. These methods have been discussed below: Methods: Using Collection.min
3 min read
How to Get Size, Minimum, and Maximum Value of Data Types in Java? The size of a data type is given by (name of datatype).SIZE. The maximum value that it can store is given by (Name of data type).MAX_VALUE. The minimum value that it can store is given by (Name of data type).MIN_VALUE. Always write first word of data type in capital. Example: 1. If you want to print
2 min read
Finding Maximum Element of Java ArrayList 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
2 min read
Finding Minimum And Maximum Element of Vector using Comparable Interface in Java The Vector class in java implements a dynamic array i.e. it can grow and shrink according to the elements that we insert or remove to/from it. It implements the List interface so it supports all the methods provided by the List interface. In this article, we are going to discuss how we can find the
3 min read
How to Find the Minimum or Maximum Element from LinkedHashSet in Java? Given a LinkedHashSet the task is to find the minimum and maximum element from this LinkedHashSet in Java. There are two ways to find the minimum or maximum element from LinkedHashSet in Java as follows Using the min and max method of the Collections classBy Iterating the LinkedHashSetExamples: Inpu
2 min read
Java Program to Find Maximum Odd Number in Array Using Stream and Filter Java 8 introduced some great features like Stream and Filter which tremendously simplify tasks like reading data and performing operations like minimum, maximum, sum, and conditional check on them. In this program, we will get the maximum of all odd numbers from a list of integers with the help of t
3 min read
Java Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix
3 min read
Java Program to Minimize the Maximum Element of an Array Given two integers N and K. Create an array of N positive integers such that the sum of all elements of the array is divisible by K and the maximum element in the array is the minimum possible. You have to find the Maximum element of that array. Example: Input : N=5, K=11 Output : 3 Explanation : We
4 min read
Finding Maximum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic
3 min read
Finding Minimum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic
3 min read