In Java, Collections.min(coll) returns the smallest element of a collection according to natural order. Like max, it scans the elements once, accepts any Collection, and does not change it.
Collections.min(coll, comparator) uses your own definition of "smallest", such as the shortest name or the entry with the lowest value.
A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.min(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Pass a non-empty collection, and optionally a Comparator.
Natural order for strings is by character code, so every uppercase letter is "smaller" than every lowercase letter. ISO dates written as yyyy-MM-dd strings compare correctly as text, which makes min a quick way to find the earliest one.
Library declarations
Java
// java.util.Collections
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
Parameters
Parameter
Description
coll
The collection to examine. Must not be empty.
comp
The comparator that defines the order. null means natural order.
Values are a collection.stock.values() is a Collection<Integer>, so min returns 3 directly.
Keep the key. To know which fruit is low, search the entry set instead of the values.
Compare entries by value.Map.Entry.comparingByValue() (Java 8) orders entries by their counts, so the pears entry is the minimum.
Frequently Asked Questions
The smallest element of the collection, using natural order or the given comparator.
min throws NoSuchElementException.
Natural String order compares character codes, and all uppercase letters have smaller codes than lowercase letters. Use String.CASE_INSENSITIVE_ORDER to ignore case.