Find Minimum Maximum value in ArrayList Java example shows how to find the minimum and maximum value in ArrayList. The example also shows how to find minimum maximum value along with index in Java ArrayList.
How to find the Minimum Maximum value in ArrayList?
There are a couple of ways to find minimum and maximum value in Java ArrayList.
1) Find Min Max value in ArrayList using Collections class
You can use min
and max
methods of Collections class.
1 |
static <T extends Object & Comparable<? super T> min(Collection<? extends T> c) |
This method returns the minimum element/value of the specified collection according to the natural ordering of the elements.
1 |
static <T extends Object & Comparable<? super T> max(Collection<? extends T> c) |
This method returns the maximum element/value of the specified collection according to the natural ordering of the elements.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Collections; public class FindMinMaxValueArrayListExample { public static void main(String[] args) { /* * ArrayList containing student marks */ ArrayList<Integer> aListMarks = new ArrayList<Integer>(); //add elements to ArrayList aListMarks.add(53); aListMarks.add(67); aListMarks.add(89); aListMarks.add(43); aListMarks.add(87); aListMarks.add(71); aListMarks.add(63); aListMarks.add(45); aListMarks.add(69); aListMarks.add(53); /* * To find minimum value in ArrayList, use * min method of Collections class. */ System.out.println( "ArrayList Min Value: " + Collections.min(aListMarks) ); /* * To find maximum value in ArrayList, use * max method of Collections class. */ System.out.println( "ArrayList Max Value: " + Collections.max(aListMarks) ); } } |
Output
1 2 |
ArrayList Min Value: 43 ArrayList Max Value: 89 |
How to find an index of minimum maximum elements in Java ArrayList?
If you want to find index of minimum or maximum element instead of value, you can use indexOf
method of the ArrayList class.
1 |
int indexOf(Object o) |
This method returns the index of the specified element. If the element is not found in the ArrayList, it returns -1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
ArrayList<Integer> aListMarks = new ArrayList<Integer>(); //add elements to ArrayList aListMarks.add(53); aListMarks.add(67); aListMarks.add(89); aListMarks.add(43); aListMarks.add(87); aListMarks.add(71); aListMarks.add(63); aListMarks.add(45); aListMarks.add(69); aListMarks.add(53); /* * To find minimum value in ArrayList, use * min method of Collections class. * * To get the index of an element, use indexOf method. */ System.out.println( "ArrayList Min Value is at index: " + aListMarks.indexOf(Collections.min(aListMarks)) ); /* * To find maximum value in ArrayList, use * max method of Collections class. * To get the index of an element, use indexOf method. */ System.out.println( "ArrayList Max Value is at index: " + aListMarks.indexOf(Collections.max(aListMarks)) ); |
Output
1 2 |
ArrayList Min Value is at index: 3 ArrayList Max Value is at index: 2 |
2) Find Min Max value in ArrayList using for loop
If you want to find min max values without using the Collections class, you can use for loop to find the same as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class FindMinMaxValueArrayListExample { public static void main(String[] args) { /* * ArrayList containing student marks */ ArrayList<Integer> aListMarks = new ArrayList<Integer>(); //add elements to ArrayList aListMarks.add(53); aListMarks.add(67); aListMarks.add(89); aListMarks.add(43); aListMarks.add(87); aListMarks.add(71); aListMarks.add(63); aListMarks.add(45); aListMarks.add(69); aListMarks.add(53); //declare min and max value as the first element of the list int min = aListMarks.get(0); int max = aListMarks.get(0); //declare min and max elements index as 0 (i.e. first element) int minIndex = 0, maxIndex = 0; //Iterate through ArrayList for(int i = 1; i < aListMarks.size(); i++ ){ /* * If current value is less than min value, it * is new minimum value */ if( aListMarks.get(i) < min ){ min = aListMarks.get(i); minIndex = i; } /* * If current value is greater than max value, it * is new maximum value. */ if( aListMarks.get(i) > max ){ max = aListMarks.get(i); maxIndex = i; } } System.out.println("ArrayList Min Value is: " + min + ", Found at index: " + minIndex ); System.out.println("ArrayList Max Value is: " + max + ", Found at index: " + maxIndex ); } } |
Output
1 2 |
ArrayList Min Value is: 43, Found at index: 3 ArrayList Max Value is: 89, Found at index: 2 |
This example is a part of the Java ArrayList tutorial with examples.
Please let me know your views in the comments section below.
What if there are duplicate entries in the ArrayList.. like 5 times number 1 ? This code will find only the first occurrence of number 1 and will say that’s the minimum as its the only one.. How to find all the minimum indexes???
Hello Simon,
Even if the ArrayList has duplicates, the minimum value above code finds remains the same. So if ArrayList has 5 times 1, the minimum value is still 1 regardless of the index and that was the purpose of the above code, to find the minimum value.
However, if you want to find index of all the minimum elements, that can be easily done by changing the code a little bit. Once you get the minimum element, scan through the ArrayList and check what all indexes has the element.
I hope this answers your question.
Thanks.