一、Stream流
- 是操作集合或数组的一种流,将数组或集合的数据放到流里面的操作流是不能保存数据的
- 流的分类:
-
- 串行流:Stream()方法是获取串行流(顺序流),适合于单线程操作
- 并行流:parallelStream()获取并行流;适合多线程操作
-
- 1.Arrays.stream(数组);
- 2.集合对象.stream()
- 3.Stream.of()
integer[] nums = {1, 4, 7, 9};
Arrays<integer> Stream = Arrays.stream(nums);
Arrys.asList(1, 3, 5, 7, 9);
Stream<Integer> Stream1 = list.stream(nums);
Stream<Integer>integerStream = Stream.of(1, 3, 5, 7, 9);
-
- limit(数字):限制显示的条数
- skip(long n):跳过几条数据
- sorted():升序排列
- sorted(Comparator):降序排列
- filter():过滤条件,将不要的数据过滤掉
- map():用来切片(映射):将一个对象的一部分数据取来
- distinct():将流里面重复的数据清除
-
- forEach():遍历流中的数据
- count():统计流里面的个数
- collect():收集,将流里面的数据收集起来
-
-
- Collectors:收集器
- toList():转为List集合
- toSet():转为set集合
二、并行流