stream()
java8新特性
一、lambda:函数型接口的匿名实现
()->{ } 括号就是接口方法的括号,接口方法如果有参数,也需写参数,一个参数时括号可省略,->分割形参列表与函数体,{} : 如果代码体只有一行代码就可以省略掉花括号,并且如果方法需要有返回值连return关键词都可以省略,系统会自动将这一行代码的结果返回。
二、stream():
- 
1 List<Teacher> list = new ArrayList<>(); 2 list.add(Teacher.builder().age(28).name("李四").build()); 3 list.add(Teacher.builder().age(27).name("张三").build()); 4 list.add(Teacher.builder().age(29).name("王五").build()); 
list.stream().map() 不影响原数据,对原数据改造后生成新数据
- 
List<Teacher> listCopy = list.stream().map(teacher -> { if ("王五".equals(teacher.getName())) { Address address = Address.builder().address("南京").build(); teacher.setAddress(address); } return teacher; }).collect(Collectors.toList()); System.out.println("产生新的List:" + listCopy); 
list.stream().forEach() 适合只对原数据读操作
- 
list.stream().forEach(teacher -> { if ("王五".equals(teacher.getName())) { Address address = Address.builder().address("南京").build(); teacher.setAddress(address); } }); System.out.println("还是老的List:" + list); 
list.stream().filter() 过滤,按条件筛选出结果
List<Teacher> listFilter = list.stream().filter( teacher -> AllTypeUtils.isNotEmptyAndNotNull(teacher.getAddress() )).collect(Collectors.toList()); System.out.println("产生新的List:" + listFilter);
三、collect(Collectors.toMap(ProInsDutyRelation::getcId, a -> a, (k1, k2) -> k1));
shiftMap = shiftTimeQuantumList.stream().filter(t -> t.getShiftTimeQuantumId() != null).collect(Collectors.toMap(ShiftTimeQuantum::getShiftTimeQuantumId, k1 -> k1, (k1, k2) -> k1));
参数解析:
     第一个参数ProInsDutyRelation::getcId表示选择ProInsDutyRelation的getcId作为map的key值;
     第二个参数a -> a表示选择将原来的对象作为map的value值;
     第三个参数(k1, k2) -> k1中,如果k1与k2的key值相同,选择k1作为那个key所对应的value值
四、分组
list.stream().collect(Collectors.groupingBy(对象Vo::分组标志字段));
Map<String, List<Student>> collect = stuList.stream().collect(Collectors.groupingBy(Student::getClassId)); for(Map.Entry<String, List<Student>> stuMap:collect.entrySet()){ String classId = stuMap.getKey(); List<Student> studentList = stuMap.getValue(); System.out.println("classId:"+classId+",studentList:"+studentList.toString()); }
五、排序
if(StringUtils.isNotBlank(carAlarmQueryDto.getSort())){ if(StringUtils.isNotBlank(carAlarmQueryDto.getOrder()) && carAlarmQueryDto.getOrder().equals("desc")){ if(StringUtils.isNotBlank(carAlarmQueryDto.getSort())){ if(carAlarmQueryDto.getSort().equals("userInfoCode")){ listRecords = listRecords.stream().sorted(Comparator.comparing(CarAlarmVo::getUserInfoCode).reversed()).collect(Collectors.toList()); }else if(carAlarmQueryDto.getSort().equals("cardCode")){ listRecords = listRecords.stream().sorted(Comparator.comparing(CarAlarmVo::getCardCode).reversed()).collect(Collectors.toList()); }else if(carAlarmQueryDto.getSort().equals("endDateTime")){ listRecords = listRecords.stream().sorted(Comparator.comparing(CarAlarmVo::getEndDateTime,Comparator.nullsFirst(String::compareTo)).reversed()).collect(Collectors.toList()); }else { } } }else if(StringUtils.isNotBlank(carAlarmQueryDto.getOrder()) && carAlarmQueryDto.getOrder().equals("asc")){ if(StringUtils.isNotBlank(carAlarmQueryDto.getSort())){ if(carAlarmQueryDto.getSort().equals("userInfoCode")){ listRecords = listRecords.stream().sorted(Comparator.comparing(CarAlarmVo::getUserInfoCode)).collect(Collectors.toList()); }else if(carAlarmQueryDto.getSort().equals("cardCode")){ listRecords = listRecords.stream().sorted(Comparator.comparing(CarAlarmVo::getCardCode)).collect(Collectors.toList()); }else if(carAlarmQueryDto.getSort().equals("endDateTime")){ listRecords = listRecords.stream().sorted(Comparator.comparing(CarAlarmVo::getEndDateTime,Comparator.nullsFirst(String::compareTo))).collect(Collectors.toList()); }else { } } }else{ } }
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号