排序文章中方法适用于 Java 1.8 及以上版本
//按年龄排序 List筛选collect = list.stream() .sorted((e1, e2) -> { //先按年龄排序 return e1.getAge().compareTo(e2.getAge()); }) .collect(Collectors.toList());
List去重collect = list.stream() //选出年龄大于35岁的 .filter((e) -> e.getAge() > 35) //只要前2个 .limit(2) .collect(Collectors.toList());
List多字段去重hospitalDepartmentList = hospitalWardVos.stream() .collect(Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing( HospitalWardVo::getWardAttributesId))), ArrayList::new) );
ListList 中单独返回某个属性值departmentClasss = result.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing( o -> o.getHospitalId() + ";" + o.getStandardDepartmentClassId() ))), ArrayList::new));
ListList 返回新对象wardIds = userWardDepartments.stream().map( UserWardDepartment::getId).collect(Collectors.toList() );
List



