Open In App

Collections min() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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);
        }
    }
}


Output:

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);
        }
    }
}


Output:

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);
        }
    }
}


Output:

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);
        }
    }
}


Output:

List: []
Trying to get the minimum value with empty list
Exception thrown : java.util.NoSuchElementException


Last Updated : 10 Oct, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads