Java Collections.min
Inspect all prices and return the smallest value in the basket.
A simple example
Collections.min
- Start with 3, 1, 2
- Do this Keep the smallest
- Result 1
Syntax
java.util.Collections.min(prices)
Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaFind the cheapest item in a basket
A shopper compares the prices in a basket before paying. Work from the prices actually present in the list so a total or minimum reflects the same set of items as the receipt.
class Main {
public static void main(String[] args) throws Exception {
var prices = java.util.List.of(8, 3, 12, 3);
System.out.println(java.util.Collections.min(prices));
}
}
3
The least expensive item costs three units. A tie still gives a single minimum price.
Count packs on the delivery slip
A loading desk receives four item counts in sequence. The supervisor needs to flag the smallest recorded count for review. Which count should the report show?
Review print quantities
A print room records four job quantities, including a cancelled job. The supervisor needs to flag the smallest recorded count for review. Which count should the report show?
What to watch for
Handle an empty basket before requesting a minimum. Empty-input behaviour varies by language.