创建stream的三种方式
一、通过集合创建Stream
(1)顺序流
List<Employee> employees = EmployeeData.getEmployees();
Stream<Employee> stream = employees.stream();
(2)并行流
Stream<Employees> employeeStream = employees.parallelStream();
二、通过数组创建
int[] arr = new int[]{1,2,3,4,5,6};
IntStream stream1 = Arrays.stream(arr);
三、stream类本身
Stream<Integer> stream2 = Stream.of(1,2,3,4,5,6);
四、创建无限流
(1)迭代
//遍历前十个偶数
Stream.iterator(0,t -> t + 2).limit(10).forEach(System.out::println);
(2)生成
Stream.generate(Math::random).limit(10).forEach(System.out::println);
筛选与切片
limit(n)–截断流,使其元素不超过给定数量
employees.stream().limit(3).forEach(System.out::println);
skip(n)–跳过元素
employees.stream().skip(3).forEach(System.out::println);
distinct()–筛选
employees.stream().distinct().forEach(System.out::println);
映射
map(Function f)–接收一个函数作为参数,将元素转换成其他形式或提取信息。
//练习:获取员工姓名长度大于3的员工的姓名
List<Employee> employees = EmployeeData.getEmployees();
employees.stream().map(n -> n.getName()).filter(n -> n.length() > 3).forEach(System.out::println);
flatMap(Function f)–接收一个函数作为参数,将流中的每个值都换成另一个流。将所有流换成同一个流。
Stream<Character> characterStream = list.stream().flatMap(StreamAPITest1::fromStringToStream);
排序
自然排序
integers.stream().sorted().forEach(System.out::println);
定制排序
employees.stream().sorted( (e1,e2) -> Integer.compare(e1.getAge(),e2.getAge()))
.forEach(System.out::println);
匹配与查找
allMatch(Predicate)–检查是否匹配所有的元素
//练习:是否所有的员工的年龄都大于18
employees.stream().allMatch(e -> e.getAge() > 18);
anyMatch(Predicate)–检查是否至少匹配一个元素
employees.stream().anyMatch(e -> e.getSalary() > 1000);
noneMatch(Predicate)–检查是否没有匹配元素。
employees.stream().noneMatch(e -> e,getName().startsWith("雷"));
findFirst–返回第一个元素
Optional<Employee> employee = employees.stream().findFirst();
System.out.println(employee);
findAny–返回当前流中的任意元素
Optional<Employee> any = employees.stream().findAny();
System.out.println(any);
count–返回流中元素的个数
long count = employees.stream().count();
System.out.println(count);
max(Comparator c)–返回流中最大值
Optional<Double> max = employees.stream().map(e -> e.getSalary()).max(Double::compare);
System.out.println(max);
min(Comparator)–返回流中最小值
//练习:返回最低工资的员工
Optional<Employee> min = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(min);
归约
reduce(T identity,BinaryOperator)–可以将流中的元素反复结合起来,得到一个值。
//练习1:计算1-10的自然数的和
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Integer reduce = list.stream().reduce(0, Integer::sum);
System.out.println(reduce);
reduce(BinaryOperator)–可以流中元素反复结合起来,得到一个值。
List<Employee> employees = EmployeeData.getEmployees();
Optional<Double> reduce1 = employees.stream().map(e -> e.getSalary()).reduce(Double::sum);
Optional<Double> reduce2 = employees.stream().map(e -> e.getSalary()).reduce((d1,d2) -> d1 + d2);
System.out.println(reduce1);
System.out.println(reduce2);
收集
collection(Collector c)–将流转换为其他形式
//练习1:查找工资大于6000的员工,结果返回一个List或Set
List<Employee> employees = EmployeeData.getEmployees();
List<Employee> collect = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toList());
collect.forEach(System.out::println);