30 lines
870 B
Java
30 lines
870 B
Java
|
package stream;
|
||
|
|
||
|
import java.util.Comparator;
|
||
|
import java.util.Optional;
|
||
|
import java.util.stream.Stream;
|
||
|
|
||
|
public class StreamExample05 {
|
||
|
public static void main(String[] args) {
|
||
|
// 计数
|
||
|
long count = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||
|
.count();
|
||
|
System.out.println(count);// 9
|
||
|
|
||
|
// 求和
|
||
|
long sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||
|
.mapToLong(Integer::longValue)
|
||
|
.sum();
|
||
|
System.out.println(sum);
|
||
|
|
||
|
// 查找最小值
|
||
|
Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||
|
.min(Comparator.comparing(i -> i))
|
||
|
.ifPresent(System.out::println);
|
||
|
|
||
|
// 查找最大值
|
||
|
Optional<Integer> max = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||
|
.max(Comparator.comparing(i -> i));
|
||
|
System.out.println(max.get());
|
||
|
}
|
||
|
}
|