java stream常用方法

1、筛选数据

单条

Student student = students.stream().filter(o -> o.getStuNo.equals(1001)).findAny().orElse(null);

.orElse(null) 表示如果没有符合条件的学生则返回null

多条

List<Integer> stuNoList = students.stream().map(Student::getStuNo).collect(Collectors.toList);

或者

List<Employee> newList = list.stream().filter(item -> {
return item.getSalary().compareTo(new BigDecimal(10000)) > 0 && !item.getWorkType().equals("项目经理");
}).collect(Collectors.toList());

或者

List<Map<String, Object>> tempDeviceDataList = deviceDataList.stream().filter(map -> map.get("sn").toString().equals(sn)).collect(Collectors.toList());

 

posted @ 2023-08-24 09:41  xxm_0122  阅读(10)  评论(0编辑  收藏  举报