Java 8 Stream 常用 API 的简单使用
1、Stream 的常用API
统计如下:
filter(按照条件过滤需要数据)
max(取出流中的最大值)
min(取出流中的最小值)
count(取出流中的数量)
sum(取出流中数据的和)
average(取出流中数据的平均值)
distinct(将流中的数据去重)
sorted(自然排序,默认为升序,可以设置为升序排序或者降序排序)
limit,skip (限制和跳过:可以流数据的部分截取,可用于后台的分页场景)
map(映射转换)
collect,toList(不可以对集合去重)
collect, toSet(可以集合去重)
toArray(将流数据转为数组)
mapToInt,distinct(将流数据转成IntStream,并去重)
reduce 求和
reduce 求最大值
reduce 求最小值
reduce 求乘积
findFirst(查找第一个元素)
findAny(任意查找一个元素)
allMatch(判断是否全部满足条件,全部满足返回 true,否则返回false)
anyMatch(判断是否有一个满足条件,只要有一个满足就返回 true,否则都不满足返回false)
noneMatch(判断是否都不满足条件,都不满足返回true,否则返回false)
flatmap(扁平化流处理)
2、示例代码
代码如下:
package mirale.luna.lambda.stream; import java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Created by Miracle Luna on 2020/12/23 */ public class StreamAPI { public static void main(String[] args) { System.out.println("===================== filter ===================="); // filter(按照条件过滤需要数据) Integer[] intArr = {1,2,3,4,5,6}; Arrays.asList(intArr).stream().filter(a -> a%2==0).forEach(System.out::println); System.out.println("===================== filter ===================="); String[] strs = {"java", "go"}; Arrays.asList(strs).stream().filter(x -> x.length() > 2).forEach(System.out::println); System.out.println("===================== filter ===================="); // max(取出流中的最大值) System.out.println("max is: " + Arrays.asList(intArr).stream().max((a, b) -> a - b).get()); // min(取出流中的最小值) System.out.println("min is: " + Arrays.asList(intArr).stream().min((a, b) -> a - b).get()); // count(取出流中的数量) System.out.println("count is: " + Arrays.asList(intArr).stream().count()); // sum(取出流中数据的和) System.out.println("sum is: " + Arrays.asList(intArr).stream().mapToInt(x -> x).sum()); // average(取出流中数据的平均值) System.out.println("average is: " + Arrays.asList(intArr).stream().mapToInt(x -> x).average().getAsDouble()); System.out.println("===================== distinct ===================="); // distinct(将流中的数据去重) Arrays.asList(1,2,3,4,3,2,5).stream().mapToInt(x -> x).distinct().forEach(System.out::println); System.out.println("===================== sorted1 ===================="); // sorted(自然排序,默认为升序) Arrays.asList(7,1,3,2,4).stream().sorted().forEach(System.out::println); System.out.println("===================== sorted2 ===================="); // sorted(设置为升序排序) Arrays.asList(7,1,3,2,4).stream().sorted((a, b) -> a - b).forEach(System.out::println); System.out.println("===================== sorted3 ===================="); // sorted(设置为降序排序) Arrays.asList(7,1,3,2,4).stream().sorted((a, b) -> b - a).forEach(System.out::println); System.out.println("===================== limit,skip ===================="); // limit,skip (限制和跳过:实现截取5-10的数据) Stream.iterate(1, i -> i+1).limit(10).skip(4).forEach(System.out::println); System.out.println("===================== map ===================="); // map(映射转换,获取偶数列表信息) Arrays.asList(1,2,3,4,5).stream().filter(i -> i%2==0).map(x->x).forEach(System.out::println); System.out.println("===================== collect toList ===================="); // collect toList(不可以对集合去重) Arrays.asList(1,2,2,4,3,4,5).stream().filter(i -> i%2==0).collect(Collectors.toList()).forEach(System.out::println); System.out.println("===================== collect toSet ===================="); // collect toSet(可以集合去重) Arrays.asList(1,2,2,4,3,4,5).stream().filter(i -> i%2==0).collect(Collectors.toSet()).forEach(System.out::println); System.out.println("===================== toArray ===================="); // toArray(将流数据转为数组) Arrays.stream(Arrays.asList(1,2,2,4,3,4,5).stream().filter(i -> i%2==0).toArray()).forEach(System.out::println); System.out.println("===================== mapToInt distinct ===================="); // mapToInt distinct(将流数据转成IntStream,并去重) Arrays.stream(Arrays.asList(1,2,2,4,3,4,5).stream().filter(i -> i%2==0).toArray()).mapToInt(x->(Integer)x).distinct().forEach(System.out::println); System.out.println("===================== reduce ===================="); // reduce 求和 System.out.println("reduce 求和: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().reduce((a, b) -> a + b).get()); // reduce 求最大值 System.out.println("reduce 求最大值: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().reduce((a, b) -> a > b ? a : b).get()); // reduce 求最小值 System.out.println("reduce 求最小值: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().reduce((a, b) -> a < b ? a : b).get()); // reduce 求乘积 System.out.println("reduce 求乘积: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().reduce((a, b) -> a * b).get()); System.out.println(Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().reduce(5, (a, b) -> a * b)); // findFirst(查找第一个元素) System.out.println("findFirst: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().findFirst().get()); // findAny(任意查找一个元素) System.out.println("findAny: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().findAny().get()); // allMatch(判断是否全部满足条件,全部满足返回 true,否则返回false) System.out.println("allMatch: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().allMatch(x -> x > 1)); // anyMatch(判断是否有一个满足条件,只要有一个满足就返回 true,否则都不满足返回false) System.out.println("anyMatch: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().anyMatch(x -> x > 1)); // noneMatch(判断是否都不满足条件,都不满足返回true,否则返回false) System.out.println("nonMatch: " + Arrays.asList(1, 2, 2, 4, 3, 4, 5).stream().noneMatch(x -> x > 1)); System.out.println("======================================"); // map 和 flatmap(扁平化流处理) String[] words = {"Hello", "World"}; Arrays.stream(words).map(word -> word.split("")).flatMap(Stream::of).distinct().forEach(System.out::print); System.out.println(" ======================================"); Stream.of(words).map(word -> word.split("")).flatMap(Arrays::stream).distinct().forEach(System.out::print); System.out.println(" ======================================"); Stream.of(words).flatMap(word -> Stream.of(word.split(""))).distinct().forEach(System.out::print); } }
运行结果如下:
===================== filter ==================== 2 4 6 ===================== filter ==================== java ===================== filter ==================== max is: 6 min is: 1 count is: 6 sum is: 21 average is: 3.5 ===================== distinct ==================== 1 2 3 4 5 ===================== sorted1 ==================== 1 2 3 4 7 ===================== sorted2 ==================== 1 2 3 4 7 ===================== sorted3 ==================== 7 4 3 2 1 ===================== limit,skip ==================== 5 6 7 8 9 10 ===================== map ==================== 2 4 ===================== collect toList ==================== 2 2 4 4 ===================== collect toSet ==================== 2 4 ===================== toArray ==================== 2 2 4 4 ===================== mapToInt distinct ==================== 2 4 ===================== reduce ==================== reduce 求和: 21 reduce 求最大值: 5 reduce 求最小值: 1 reduce 求乘积: 960 4800 findFirst: 1 findAny: 1 allMatch: false anyMatch: true nonMatch: false ====================================== HeloWrd ====================================== HeloWrd ====================================== HeloWrd Process finished with exit code 0
PS:
希望本篇博文对小伙伴有所帮助。
有兴趣的话,可以加个关注,互相学习。
谢谢~