Java中IntStream min()方法
Java IntStream 类中的 IntStream min() 方法用于从流中获取最小元素。它返回一个描述此流的最小元素的 OptionalInt,或如果此流为空,则返回一个空可选项。
语法如下
OptionalInt min()
其中,OptionalInt 是一个容器对象,可能包含 int 值,也可能不包含。
创建 IntStream 并添加一些元素
IntStream intStream = IntStream.of(89, 45, 67, 12, 78, 99, 100);
现在,获取流的最小元素
OptionalInt res = intStream.min();
以下是 Java 中实现 IntStream min() 方法的一个示例。OptionalInt 类的 isPresent() 方法在存在值时返回 true
示例
import java.util.*;
import java.util.stream.IntStream;
public class Demo {
public static void main(String[] args) {
IntStream intStream = IntStream.of(89, 45, 67, 12, 78, 99, 100);
OptionalInt res = intStream.min();
System.out.println("The minimum element of this stream:");
if (res.isPresent()) {
System.out.println(res.getAsInt());
} else {
System.out.println("Nothing!");
}
}
}输出
The minimum element of this stream: 12
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP