Java 8:Stream API 流式操作-LMLPHP


Java 8:Stream API

特性

生成流

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

流式操作

forEach

Random random = new Random();
random.ints().limit(10).forEach(System.out::println);

map

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());

filter

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
long count = strings.stream().filter(string -> string.isEmpty()).count();

limit

Random random = new Random();
random.ints().limit(10).forEach(System.out::println);

sorted

Random random = new Random();
random.ints().limit(10).sorted().forEach(System.out::println);

并行(parallel)程序

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
long count = strings.parallelStream().filter(string -> string.isEmpty()).count();

Collectors

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
 
System.out.println("筛选列表: " + filtered);
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);

统计

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
 
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
 
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());

map 操作

案例

systemApplicationTypeRepo.list(new QueryWrapper<SystemApplicationType>().orderByAsc(SystemApplicationTypeCol.ID))
                .stream().map(bean -> new ResCommonIdNameCode(bean.getId(), bean.getName(), bean.getName())).collect(Collectors.toList()
systemApplicationTypeRepo.list(...):这部分代码使用了一个名为 systemApplicationTypeRepo 的仓库(Repository),通过调用 list 方法来查询数据库中的数据;list 方法接受一个查询条件对象 QueryWrapper<SystemApplicationType> 作为参数,用于指定查询条件

new QueryWrapper<SystemApplicationType>().orderByAsc(SystemApplicationTypeCol.ID):这是创建一个查询条件对象的过程;QueryWrapper 是 MyBatis-Plus 提供的用于构建查询条件的工具;orderByAsc(SystemApplicationTypeCol.ID) 表示按照 SystemApplicationType 实体类中 ID 字段的升序进行排序。

.stream():这将查询结果转换为一个 Stream<SystemApplicationType> 对象,便于后续的操作

.map(bean -> new ResCommonIdNameCode(bean.getId(), bean.getName(), bean.getName())):这部分使用 map 操作将查询结果的每个 SystemApplicationType 对象映射为 ResCommonIdNameCode 对象;ResCommonIdNameCode 是一个自定义的类,构造函数接受 id、name 和 code 作为参数,用于创建一个新的对象

.collect(Collectors.toList()):最后,使用 collect 方法将 Stream<ResCommonIdNameCode> 对象收集为 List<ResCommonIdNameCode>,即最终的结果列表
noticeResponses.stream()
                .sorted(Comparator.comparing(SystemNoticeResponse::getReadStatus)
                        .thenComparing(Comparator.comparing(SystemNoticeResponse::getCreateAt).reversed()))
                .collect(Collectors.toList());
.stream():将 noticeResponses 列表转换为一个 Stream<SystemNoticeResponse> 对象,使其能够使用 Stream API 提供的操作

.sorted(...):这是一个中间操作,用于对流中的元素进行排序;使用 Comparator.comparing(...) 方法创建了一个比较器,用于指定排序的规则;首先,按照 SystemNoticeResponse 对象的 readStatus 字段进行升序排序;.thenComparing(...) 表示如果 readStatus 相同,则按照 createAt 字段进行降序排序(使用 reversed() 方法)

.collect(Collectors.toList()):最后,使用 collect 方法将排序后的 Stream<SystemNoticeResponse> 对象收集为一个新的 List<SystemNoticeResponse>,即排序后的 noticeResponses 列表


Java 8:Stream API 流式操作-LMLPHP

08-14 11:53