Find Min Max values with min() max()

Find Min Max values with min() max()


There comes two methods from Collections class to find the elements with minimum and maximum values of a data structure. These methods can be applied to any Collection class (subclasses of Collection interface). In the following code ArrayList is used.

Following are the method signatures.

  • static Object min(Collection col1): Returns the element with the minimum value in the Collection col1.
  • static Object max(Collection col1): Returns the element with the maximum value in the Collection col1.
Example on Find Min Max values with min() max() meethods


Find Min Max values with min() max()
Output screenshot on Find Min Max values with min() max()

ArrayList firstList = new ArrayList();
firstList.add(50); firstList.add(10); firstList.add(20);

An ArrayList object, firstList, is created with three integer elements 50, 10 and 20.

Collections.min(firstList);
Collections.max(firstList);

The min() and max() methods returns 10 and 50, the minimum and maximum values of firstList.

ArrayList secondList = new ArrayList();
secondList.add(“five”); secondList.add(“two”); secondList.add(“six”);

Another ArrayList object secondList is created with string elements "five", "two", and "six".

Collections.min(secondList);
Collections.max(secondList);

The above min() and max() methods with respect strings print five and two because f comes before t and s and last comes is t (after s). It is alphabetical order sorting.

Leave a Reply

Your email address will not be published. Required fields are marked *