In this tutorial, you will learn how to find the maximum and minimum value element from the ArrayList. Java provides direct methods to get maximum and minimum value from any collection class i.e max() and min() respectively. Here is an example that consists of ArrayList of integers and using the max and min methods of Collections class, we have find the largest and smallest values from the list.
Example:
import java.util.*;
class FindMaxMinUsingCollection
{
public static void main(String[] args){
ArrayList list = new ArrayList();
list.add(50);
list.add(15);
list.add(450);
list.add(550);
list.add(200);
Object minValue = Collections.min(list);
Object maxValue = Collections.max(list);
System.out.println("Minimum value is = " + minValue);
System.out.println("Maximum value is = " + maxValue);
}
}
Output:
Minimum value is = 15 Maximum value is = 550
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.