min(Collection<? extends T> coll)
The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
This method iterates over the entire collection, hence it requires time proportional to the size of the collection.
Syntax:
public static <T
extends Object & Comparable<? super T>> T
min(Collection<? extends T> coll)
Parameters: This method takes the collection coll as a parameter whose minimum element is to be determined
Return Value: This method returns the minimum element of the given collection, according to the natural ordering of its elements.
Exception: This method throws NoSuchElementException if the collection is empty.
Below are the examples to illustrate the min() method
Example 1:
// Java program to demonstrate // min() method // for <Integer> Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create link list object List<Integer> list = new LinkedList<Integer>(); // populate the list list.add(10); list.add(20); list.add(30); list.add(40); // printing the List System.out.println("List: " + list); // getting minimum value // using min() method int min = Collections.min(list); // printing the min value System.out.println("Minimum value is: " + min); } catch (NoSuchElementException e) { System.out.println("Exception thrown : " + e); } } } |
List: [10, 20, 30, 40] Minimum value is: 10
Example 2: To demonstrate NoSuchElementException
// Java program to demonstrate // min() method for NoSuchElementException import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create link list object List<Integer> list = new LinkedList<Integer>(); // printing the List System.out.println("List: " + list); // getting minimum value // using min() method System.out.println("Trying to get" + " the minimum value " + "with empty list"); int min = Collections.min(list); // printing the min value System.out.println("Min value is: " + min); } catch (NoSuchElementException e) { System.out.println("Exception thrown : " + e); } } } |
List: [] Trying to get the minimum value with empty list Exception thrown : java.util.NoSuchElementException
min(Collection<? extends T> coll, Comparator<? super T> comp)
The min(Collections, Comparator) method of java.util.Collections class is used to return the minimum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator .
This method iterates over the entire collection, hence it requires time proportional to the size of the collection.
Parameters: This method takes the following argument as parameters:
- coll- the collection whose minimum element is to be determined.
- comp- the comparator with which to determine the minimum element. A null value indicates that the elements’ natural ordering should be used.
Return Value: This method returns the minimum element of the given collection, according to the specified comparator.
Exception: This method throws NoSuchElementException if the collection is empty.
Below are the examples to illustrate the min() method
Example 1:
// Java program to demonstrate // min() method // for Integer import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create link list object List<Integer> list = new LinkedList<Integer>(); // populate the list list.add(10); list.add(20); list.add(30); list.add(40); // printing the List System.out.println("List: " + list); // getting minimum value // using min() method int min = Collections.min(list, Collections.reverseOrder()); // printing the min value System.out.println("Min value by reverse order is: " + min); } catch (NoSuchElementException e) { System.out.println("Exception thrown : " + e); } } } |
List: [10, 20, 30, 40] Min value by reverse order is: 40
Example 2: To demonstrate NoSuchElementException
// Java program to demonstrate // min() method for NoSuchElementException import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create link list object List<Integer> list = new LinkedList<Integer>(); // printing the List System.out.println("List: " + list); // getting minimum value // using min() method System.out.println("Trying to get" + " the minimum value " + "with empty list"); int min = Collections.min(list, Collections.reverseOrder()); // printing the min value System.out.println("Min value is: " + min); } catch (NoSuchElementException e) { System.out.println("Exception thrown : " + e); } } } |
List: [] Trying to get the minimum value with empty list Exception thrown : java.util.NoSuchElementException
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Collections max() method in Java with Examples
- Collections unmodifiableList() method in Java with Examples
- Collections checkedList() method in Java with Examples
- Collections checkedSet() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
- Collections unmodifiableMap() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections checkedCollection() method in Java with Examples
- Collections unmodifiableCollection() method in Java with Examples
- Collections unmodifiableSortedMap() method in Java with Examples
- Collections unmodifiableSet() method in Java with Examples
- Collections enumeration() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections synchronizedCollection() method in Java with Examples
- Collections synchronizedList() method in Java with Examples
- Collections synchronizedMap() method in Java with Examples
- Collections synchronizedSet() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

