For More info watch video
The Collections.min method is a part of the Java Collections Framework and is used to find the minimum element in a given collection. It utilizes the natural ordering of the elements (if they implement the Comparable interface) or a specified Comparator to determine the minimum element.
Description of Collections.min
Method Signatures
Natural Ordering
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
Parameters:
coll: The collection from which the minimum element is to be determined. All elements in the collection must implement the Comparable interface.
Returns: The minimum element according to the natural ordering.
Throws:
ClassCastException if the elements are not mutually comparable.
NoSuchElementException if the collection is empty.
Custom Comparator
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
Parameters:
coll: The collection from which the minimum element is to be determined.
comp: The comparator to determine the order of the collection. A null comparator indicates that natural ordering should be used.
Returns: The minimum element according to the specified comparator.
Throws:
NoSuchElementException if the collection is empty.
Example of Collections.min
Below are examples demonstrating how to use the Collections.min method with both natural ordering and a custom comparator.
Example 1: Using Natural Ordering
In this example, we find the minimum element from a list of integers using their natural ordering.
import java.util.*;
public class MinExampleNaturalOrdering {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(3, 9, 1, 5, 8);
// Find the minimum element using natural ordering
int minNumber = Collections.min(numbers);
System.out.println("Minimum number: " + minNumber); // Output: 1
}
}
Example 2: Using Custom Comparator
In this example, we find the minimum element from a list of strings based on their lengths using a custom comparator.
import java.util.*;
public class MinExampleCustomComparator {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Banana", "Apple", "Mango", "Cherry");
// Find the minimum element using a custom comparator based on string length
String minFruitByLength = Collections.min(fruits, Comparator.comparingInt(String::length));
System.out.println("Minimum fruit by length: " + minFruitByLength); // Output: Apple
}
}
Explanation
Natural Ordering: In the first example, Collections.min(numbers) uses the natural ordering of integers (which implements the Comparable interface) to find the minimum value.
Custom Comparator: In the second example, Collections.min(fruits, Comparator.comparingInt(String::length)) uses a custom comparator that compares the lengths of the strings to determine which string is the shortest.
The Collections.min method is versatile and can be used to find the minimum element in collections containing any type of objects, as long as the objects are comparable either naturally or through a provided comparator.
Top comments (0)