Skip to content

Find Minimum Maximum value in ArrayList

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.

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

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

Example

Output

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.

This method returns the index of the specified element. If the 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 the Collections class, you can use for loop to find the same as given below.

Output

This example is a part of the Java ArrayList tutorial with examples.

Please let me know your views in the comments section below.

About the author

2 comments

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

    1. 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.

Leave a Reply

Your email address will not be published.