Collections

Find Minimum Maximum value in ArrayList

Find Minimum Maximum value in ArrayList Java example shows how to find minimum and maximum value in ArrayList. Example also shows how to find minimum maximum value along with index in Java ArrayList.

How to find 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.

This method returns minimum element/value of the specified collection according to the natural ordering of the elements.

This method returns maximum element/value of the specified collection according to the natural ordering of the elements.

Example

Output

How to find 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 ArrayList class.

This method returns the index of specified element. If element is not found in the ArrayList, it returns -1.

Output

2)    Find Min Max value in ArrayList using for loop

If you want to find min max values without using Collections class, you can use for loop to find the same as given below.

Output

Visit ArrayList examples to learn more about ArrayList. Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Leave a Reply

avatar
newest oldest most voted
Simon
Simon

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???