5、Stream API
Stream
是 Java8
中处理集合的关键抽象概念,它可以指定你对集合的操作,可以执行查找、过滤和映射数据等操作。
Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来提供一种对 Java 集合运算和表达的高阶抽象。
5.1、流(Stream) 是什么呢?
数据通道, 是一个来自数据源的元素队列并支持聚合操作 。
注意:
- Stream 自己不会存储元素。
- Stream 不会改变源对象。相反,他们会返回一个持有结果的新Stream。
- Stream 操作是延迟执行的。这意味着他们会等到需要结果的时候才执行。
5.2、操作流的三个步骤
- 创建流
- 中间操作
- 终止操作
5.3、创建Stream
//集合
List<String> list = new ArrayList<>();
Stream<String> stream = list.stream();
//数组
IntStream stream = Arrays.stream(new int[10]);
//Stream静态方法
Stream<String> a = Stream.of("a", "2", "3");
//创建无限流
Stream<Integer> iterate = Stream.iterate(0, (x) -> x + 2);
5.4、Stream中间操作
一个Stream可以有多个中间操作,形成一个流水线,遇到终止操作依次执行中间操作。
1、filter(Predicate p)
接收 Lambda , 从流中排除某些元素
Stream<String> stream = Stream.of("abc", "122", "lemon","haha");
stream = stream.filter((e)->{
System.out.println("filter中间操作");
return e.length() > 3;
});
stream.forEach(System.out::println);
2、distinct()
筛选,通过流所生成元素的 hashCode() 和 equals() 去除重复元素
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.distinct();
stream.forEach(System.out::println);
3、limit
截断流,使其元素不超过给定数量。
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.distinct().limit(2);
stream.forEach(System.out::println);
4、skip(long n)
跳过元素,跳过前N个元素,与 limit(n) 互补
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.skip(2);
stream.forEach(System.out::println);
5、map(Function f)
接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.map((e) -> e.toUpperCase());
stream.forEach(System.out::println);
6、flatMap(Function f)
接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream.flatMap((e) -> {
String[] split = e.split("");
return Stream.of(split);
}).forEach(System.out::println);
7、sorted()
产生一个新流,其中按自然顺序排序。
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream.sorted().forEach(System.out::println);
8、sorted(Comparator comp)
产生一个新流,其中按比较器顺序排序(定制排序)
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream.sorted(Comparator.comparingInt(String::length)).forEach(System.out::println);
5.4、Stream 的终止操作
1、count()
返回流中元素总数
Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
long count = stream.sorted(Comparator.comparingInt(String::length)).count();
System.out.println(count);
2、max(Comparator c)
返回流中最大值
Stream<String> stream = Stream.of("123","1234", "12345", "123456","1234567","aaaaaaaaaa");
Optional<String> max = stream.max(Comparator.comparingInt(String::length));
System.out.println(max.get());
3、min(Comparator c)
返回流中最小值
Stream<String> stream = Stream.of("123","1234", "12345", "123456","1234567","aaaaaaaaaa");
Optional<String> min = stream.min(Comparator.comparingInt(String::length));
System.out.println(min.get());
4、forEach(Consumer c)
内部迭代
Stream<String> stream = Stream.of("123","1234", "12345", "123456","1234567","aaaaaaaaaa");
stream.forEach(System.out::println);
5、reduce(T iden, BinaryOperator b)
将流中元素结合起来,得到一个值,返回 T
Stream<Integer> stream = Stream.of(1,2,3,4,5);
int result = stream.reduce(0, (x, y) -> {
return x + y;
});
System.out.println(result);
欢迎来到testingpai.com!
注册 关于