Java Collections.min() and Collections.max() Methods
In this article, we will explore min()
and max()
methods of the Collections
utility class. In Java, the Collections
class provides a set of utility methods to manipulate and operate on collections. These two min()
and max()
methods allow you to find the minimum and maximum elements within a collection.
Java Collections.min()
The Collections.min()
method is used to find the minimum element from a collection based on the natural ordering of its elements. The method signature is as follows:
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
The min()
method takes a collection as its parameter and returns the minimum element of that collection. The collection can be of any type that extends the Comparable
interface, which means its elements must implement the Comparable
interface to define their natural ordering.
Example: Finding the minimum element in a list of integers. Let’s say we have a list of integers, and we want to find the minimum value among them:
package org.websparrow;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MinExample {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(2);
numbers.add(9);
numbers.add(1);
numbers.add(3);
Integer minNumber = Collections.min(numbers);
System.out.println("Minimum number: " + minNumber);
}
}
Output:
Minimum number: 1
In this example, we create an ArrayList
called numbers
and add some integers to it. Then we use the Collections.min()
method to find the minimum value in the list, which is 1 in this case.
Java Collections.max()
Similarly, the Collections.max()
method is used to find the maximum element from a collection based on the natural ordering of its elements. The method signature is as follows:
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
The max()
method takes a collection as its parameter and returns the maximum element of that collection. As with min()
, the collection’s elements must implement the Comparable
interface to define their natural ordering.
Example: Finding the maximum element in a set of strings. Let’s consider a scenario where we have a Set
of strings, and we want to find the maximum value based on lexicographic ordering:
package org.websparrow;
import java.util.HashSet;
import java.util.Collections;
import java.util.Set;
public class MaxExample {
public static void main(String[] args) {
Set<String> words = new HashSet<>();
words.add("Thailand");
words.add("Nepal");
words.add("India");
String maxWord = Collections.max(words);
System.out.println("Maximum word: " + maxWord);
}
}
Output:
Maximum word: Thailand
In this example, we create a HashSet
called words
and add some strings to it. By utilizing the Collections.max()
method, we find the maximum string value in the set, which is “Thailand” in this case.
Overloaded version of Collections.min() and Collections.max()
In addition to the versions explained above, there are overloaded versions of Collections.min()
and the Collections.max()
method that allow you to specify a custom Comparator
to define the ordering of elements.
The method signatures for these variants are as follows:
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
Example: Let’s consider a scenario where we have a list of strings, and we want to find the string with the minimum length and the string with the maximum length. We’ll use a custom Comparator
that compares the lengths of the strings.
package org.websparrow;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomMinMaxExample {
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
strings.add("Thailand");
strings.add("India");
strings.add("Nepal");
strings.add("USA");
// Custom Comparator based on string length
Comparator<String> lengthComparator = Comparator.comparingInt(String::length);
String shortestString = Collections.min(strings, lengthComparator);
String longestString = Collections.max(strings, lengthComparator);
System.out.println("Shortest string: " + shortestString);
System.out.println("Longest string: " + longestString);
}
}
Output:
Shortest string: USA
Longest string: Thailand
References
- Collections.max()- javaDoc
- Collections.min()- JavaDoc
- Java Stream map() vs flatMap() Method
- Java – isBlank() vs isEmpty() method of String class