The min()
method in Java is a part of the java.util.stream.Stream
interface and it is used to find the minimum element of the stream according to the provided Comparator
. In this guide, we will learn how to use min()
method in Java with practical examples and real-world use cases to better understand its usage.Table of Contents
- Introduction
min()
Method Syntax- Examples
- Basic Usage
- Using
min()
with Custom Comparator
- Real-World Use Case
- Conclusion
Introduction
The Stream.min()
method in Java is used to find the minimum element in a stream based on a given comparator. It returns an Optional
containing the smallest element if available.
This method is useful when you need to find the lowest value in a stream, such as the smallest number or alphabetically first string.
min()
is commonly used to compare and identify the smallest element in a collection or stream of data.
min() Method Syntax
The syntax for the min()
method is as follows:
Optional<T> min(Comparator<? super T> comparator)
Parameters:
comparator
: AComparator
to compare elements of the stream.
Returns:
- An
Optional<T>
describing the minimum element of the stream, or an emptyOptional
if the stream is empty.
Throws:
- This method does not throw any exceptions.
Examples
Basic Usage
To demonstrate the basic usage of min()
, we will create a Stream
of integers and use min()
to find the minimum value.
Example
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;
public class MinExample {
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(5, 2, 8, 3, 1);
// Use min() to find the minimum value
Optional<Integer> min = stream.min(Comparator.naturalOrder());
// Print the minimum value if present
min.ifPresent(System.out::println);
}
}
Output:
1
Using min()
with Custom Comparator
This example shows how to use min()
with a custom Comparator
to find the shortest string in a stream.
Example
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;
public class MinWithComparatorExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "cherry", "date");
// Use min() to find the shortest string
Optional<String> shortestString = stream.min(Comparator.comparingInt(String::length));
// Print the shortest string if present
shortestString.ifPresent(System.out::println);
}
}
Output:
date
Real-World Use Case
Finding the Employee with the Lowest Salary
In real-world applications, the min()
method can be used to find the employee with the lowest salary from a stream of employees.
Example
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;
public class MinSalaryExample {
static class Employee {
String name;
int salary;
Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
int getSalary() {
return salary;
}
@Override
public String toString() {
return name + ": " + salary;
}
}
public static void main(String[] args) {
Stream<Employee> employees = Stream.of(
new Employee("Alice", 50000),
new Employee("Bob", 60000),
new Employee("Charlie", 70000)
);
// Use min() to find the employee with the lowest salary
Optional<Employee> lowestPaidEmployee = employees.min(Comparator.comparingInt(Employee::getSalary));
// Print the employee with the lowest salary if present
lowestPaidEmployee.ifPresent(System.out::println);
}
}
Output:
Alice: 50000
Conclusion
The Stream.min()
method is used to find the minimum element of the stream according to the provided Comparator
. This method is particularly useful for determining the smallest element in a stream based on a specific comparison criterion.
By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, finding the minimum elements as needed.