一、方法概述
Stream 流提供 count 方法来统计其中的元素个数
二、案例
@Slf4j
public class StreamDemo {
public static void main(String[] args) {
List<Person> personList = Arrays.asList(
new Person(1, "大毛", 30, 175),
new Person(2, "二毛", 25, 170),
new Person(3, "三毛", 25, 170),
new Person(4, "小毛", 20, 163));
// 获取 Stream 流
Stream<Person> personStream = personList.stream();
long count = personStream.count();
log.info("流中元素的个数是: {}",count);
}
}
三、测试结果