The min()
method in Java, part of the java.util.stream.LongStream
interface, is used to find the minimum element in the stream. This method is useful when you need to determine the lowest value in a stream of long values.
Table of Contents
- Introduction
min()
Method Syntax- Understanding
min()
- Examples
- Basic Usage
- Using
min()
with Filtered Streams
- Real-World Use Case
- Conclusion
Introduction
The min()
method is a terminal operation that returns an OptionalLong
describing the minimum element of the stream, or an empty OptionalLong
if the stream is empty. This method is particularly useful for finding the lowest value in a stream of long values.
min() Method Syntax
The syntax for the min()
method is as follows:
OptionalLong min()
Parameters:
- This method does not take any parameters.
Returns:
- An
OptionalLong
describing the minimum element of the stream, or an emptyOptionalLong
if the stream is empty.
Throws:
- This method does not throw any exceptions.
Understanding min()
The min()
method processes each element of the stream to determine the minimum value. If the stream is empty, it returns an empty OptionalLong
. If the stream contains elements, it returns an OptionalLong
containing the minimum value.
Examples
Basic Usage
To demonstrate the basic usage of min()
, we will create a LongStream
and use min()
to find the minimum element.
Example
import java.util.OptionalLong;
import java.util.stream.LongStream;
public class MinExample {
public static void main(String[] args) {
LongStream stream = LongStream.of(1L, 2L, 3L, 4L, 5L);
// Use min() to find the minimum element
OptionalLong minElement = stream.min();
// Print the minimum element if present
minElement.ifPresent(System.out::println);
}
}
Output:
1
Using min()
with Filtered Streams
This example shows how to use min()
in combination with filtering to find the minimum value among elements that match a specific condition.
Example
import java.util.OptionalLong;
import java.util.stream.LongStream;
public class MinWithFilterExample {
public static void main(String[] args) {
LongStream stream = LongStream.of(10L, 20L, 30L, 40L, 50L);
// Use min() to find the minimum element greater than 25
OptionalLong minElement = stream.filter(n -> n > 25).min();
// Print the minimum element if present
minElement.ifPresent(System.out::println);
}
}
Output:
30
Real-World Use Case
Finding the Minimum Transaction Amount
In real-world applications, the min()
method can be used to find the lowest transaction amount from a stream of transaction values.
Example
import java.util.OptionalLong;
import java.util.stream.LongStream;
public class MinTransactionExample {
public static void main(String[] args) {
LongStream transactionAmounts = LongStream.of(1000L, 2000L, 1500L, 3000L, 2500L);
// Use min() to find the lowest transaction amount
OptionalLong minTransaction = transactionAmounts.min();
// Print the lowest transaction amount if present
minTransaction.ifPresent(amount -> System.out.println("Lowest Transaction Amount: " + amount));
}
}
Output:
Lowest Transaction Amount: 1000
Conclusion
The LongStream.min()
method is used to find the minimum element in the stream. This method is particularly useful for determining the lowest value in a stream of long values. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that the minimum value is correctly identified.
Comments
Post a Comment
Leave Comment