The min() method in Java is a utility method provided by the java.util.Collections class. It is used to find the minimum element of a given collection. The method comes in two variants: one that uses the natural ordering of the elements and another that uses a specified comparator to determine the order.
Table of Contents
- Introduction
min()Method Syntax- Examples
- Basic Usage of
min()with Natural Ordering - Using
min()with Custom Comparator
- Basic Usage of
- Real-World Use Case
- Conclusion
Introduction
The Collections.min() method is used to find the minimum element in a collection. The method works with any collection that implements the Collection interface, such as List, Set, etc. The element with the smallest value is returned based on either its natural ordering or a custom comparator provided by the user.
The natural ordering is determined by the compareTo() method of the Comparable interface, which must be implemented by the elements in the collection. Alternatively, a Comparator can be provided to define a custom ordering logic.
min() Method Syntax
Variant 1: Natural Ordering
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
-
Parameters:
coll: The collection whose minimum element is to be determined, based on natural ordering.
-
Returns:
- The minimum element of the given collection according to its natural ordering.
-
Throws:
ClassCastExceptionif elements in the collection are not mutually comparable.NoSuchElementExceptionif the collection is empty.NullPointerExceptionif the collection is null or contains null elements.
Variant 2: Custom Comparator
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
-
Parameters:
coll: The collection whose minimum element is to be determined, based on the specified comparator.comp: The comparator to determine the order of the elements.
-
Returns:
- The minimum element of the given collection according to the order induced by the specified comparator.
-
Throws:
ClassCastExceptionif the comparator is incompatible with the elements in the collection.NoSuchElementExceptionif the collection is empty.NullPointerExceptionif the collection or comparator is null or if the collection contains null elements.
Examples
Basic Usage of min() with Natural Ordering
The following example demonstrates how to use the min() method to find the minimum element in a list of integers using natural ordering.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MinExample {
public static void main(String[] args) {
// Create a list of integers
List<Integer> numbers = new ArrayList<>();
Collections.addAll(numbers, 10, 20, 30, 5, 15);
// Find the minimum element using natural ordering
Integer minNumber = Collections.min(numbers);
// Display the minimum element
System.out.println("Minimum number: " + minNumber);
}
}
Output:
Minimum number: 5
Using min() with Custom Comparator
You can also use the min() method with a custom comparator to determine the minimum element according to a specific order.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class CustomMinExample {
public static void main(String[] args) {
// Create a list of students
List<Student> students = new ArrayList<>();
students.add(new Student("Amit", 20));
students.add(new Student("Neha", 22));
students.add(new Student("Raj", 19));
// Define a comparator to compare students by age
Comparator<Student> ageComparator = Comparator.comparingInt(student -> student.age);
// Find the minimum element using the custom comparator
Student youngestStudent = Collections.min(students, ageComparator);
// Display the minimum element
System.out.println("Youngest student: " + youngestStudent);
}
}
Output:
Youngest student: Raj (19)
Explanation:
-
Natural Ordering: In the first example, the
min()method uses the natural ordering of integers to find the smallest number in the list. -
Custom Comparator: In the second example, a custom comparator is used to compare students by their age, allowing the
min()method to determine the youngest student in the list.
Real-World Use Case
Finding the Lowest Priced Product
In real-world applications, the min() method can be used to find the lowest-priced product from a list of products. This is particularly useful in e-commerce platforms and recommendation systems.
Example
Imagine a scenario where you need to find the product with the lowest price from a list of products.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Product {
String name;
double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return name + " (Price: $" + price + ")";
}
}
public class ProductExample {
public static void main(String[] args) {
// Create a list of products
List<Product> products = new ArrayList<>();
products.add(new Product("Laptop", 1500.00));
products.add(new Product("Smartphone", 800.00));
products.add(new Product("Tablet", 600.00));
// Define a comparator to compare products by price
Comparator<Product> priceComparator = Comparator.comparingDouble(product -> product.price);
// Find the minimum element using the custom comparator
Product lowestPricedProduct = Collections.min(products, priceComparator);
// Display the lowest-priced product
System.out.println("Lowest Priced Product: " + lowestPricedProduct);
}
}
Output:
Lowest Priced Product: Tablet (Price: $600.0)
Explanation:
-
Product List: The list contains products with their respective prices.
-
Custom Comparator: The comparator compares products by their price, allowing the
min()method to find the product with the lowest price.
Conclusion
The Collections.min() method is a powerful utility for finding the minimum element in a collection in Java. By providing a simple way to determine the smallest element based on natural ordering or a custom comparator, it enhances the flexibility and readability of your code. This method is particularly valuable in scenarios where you need to identify the minimum element within collections, improving the robustness and maintainability of your Java applications.