Collections min() method in Java with Examples
Last Updated :
10 Oct, 2018
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
// 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
// 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
// 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
// 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
Similar Reads
Collections max() method in Java with Examples max(Collection<? extends T> coll) The max() method of java.util.Collections class is used to return the maximum 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
5 min read
Collections list() method in Java with Examples The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that requi
2 min read
Collection add() Method in Java with Examples The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
4 min read
Collections fill() method in Java with Examples The fill() method of java.util.Collections class is used to replace all of the elements of the specified list with the specified element. This method runs in linear time. Syntax: public static void fill(List list, T obj) Parameters: This method takes following argument as parameter list - the list t
2 min read
Collectors toList() method in Java with Examples The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c
2 min read
Collection clear() method in Java with Examples The collection clear() method of Java Collection Interface clears the Collection upon which it is called. After this method is called, the collection will be empty as it removes all the elements from the collection. This method does not take any parameter and does not return any value. Example: Java
3 min read
Collections addAll() method in Java with Examples The addAll() method of java.util.Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this
3 min read
Collections singletonList() method in Java with Examples The singletonList() method of java.util.Collections class is used to return an immutable list containing only the specified object. The returned list is serializable. This list will always contain only one element thus the name singleton list. When we try to add/remove an element on the returned sin
2 min read
Collection addAll() method in Java with Examples The addAll(Collection collection) of java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false. Syntax:
3 min read
Collection contains() method in Java with Examples The contains(Object element) of java.util.Collection interface is used to check whether the element 'element' exists in this collection. This method returns a boolean value depicting the presence of the element. If the element is present, it returns true, else it returns false. Syntax: Collection.co
3 min read