java 8中利用stream()对list进行遍历、过滤、排序

1.获取list中id的集合

List<Integer> lstVideoID = listVideoLine.stream().map(x -> x.getVideoId()).collect(Collectors.toList());

2.list通过过滤条件遍历

List<VideoGis> selectList = lstVideoGis.stream().filter(x-> x.getLineID().equals(d.getLineId())).collect(Collectors.toList());

3.排序

对年龄升序排序

//按年龄排序(Integer类型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());

对年龄进行降序排序

//按年龄排序(Integer类型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());

对年龄降序排序,年龄一样的在根据身高升序排序

 List<StudentInfo> studentsSortName = studentList.stream()
            .sorted(Comparator.comparing(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight)).collect(Collectors.toList());

 


版权声明:本文为m0_37914467原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。