Java Collections min() Method
The min() is a Java Collections class method which returns the minimum value for the given inputs. There is two different types of Java min() method which can be differentiated depending on its parameter. These are:
- Java Collections min(coll) Method
- Java Collections min(coll, comp) Method
Java Collections min(coll) Method
The min() method of Java Collections class is used to get the minimum element of the given collection, according to the natural ordering of its elements.
Java Collections min(coll, comp) Method
The min() method of Java Collections class is used to get the minimum element of the given collection, according to the order induced by the specified comparator.
Syntax
Following is the declaration of min() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
coll | It is the collection whose minimum element is to be determined. | Required |
comp | It is the the comparator with which we determine the minimum element. | Required |
Returns
Method | Returns |
---|---|
min(Collection<? extends T> coll) | It returns the minimum element of the given collection, according to the natural ordering of its elements. |
min(Collection<? extends T> coll, Comparator<? super T> comp) | It returns the minimum element of the given collection, according to the order of the specified comparable. |
Exceptions
The min() method throws the following exceptions-
ClassCastException– It throws this exception if the collection contains elements that are not mutually comparable (for example, strings and integers).
NoSuchElementException– It throws this exception if the collection is empty.
Compatibility Version
Java 1.4 and above
Example 1
Output:
Minimum character is: E
Example 2
Output:
Minimum element is: 10
Example 3
Output:
Min val: -80 Min val in reverse order: 40
Example 4
Output:
Mininum character is: COBOL