limit 方法会限制输出值个数,入参是限制的个数大小,demo 如下:
public void testLimit(){
List<String> beforeCodes = students.stream().map(StudentDTO::getCode).collect(Collectors
log.info("TestLimit 限制之前学生的学号为 {}",JSON.toJSONString(beforeCodes));
List<String> limitCodes = beforeCodes.stream()
.limit(2L)
.collect(Collectors.toList());
log.info("TestLimit 限制最大限制 2 个学生的学号 {}",JSON.toJSONString(limitCodes));
// 直接连起来写
List<String> codes = students.stream()
.map(StudentDTO::getCode)
.limit(2L)
.collect(Collectors.toList());
log.info("TestLimit 限制最大限制 2 个学生的学号 {}",JSON.toJSONString(codes));
}
输出结果如下: