1 //筛选与切片 filter:过滤出符合条件的元素 limit:使其元素不超过一定值。 skip:跳过前面一定数量的元素 distinct :去除重复数据,要重写hashcode和equals方法
2 //映射 map :根据规则将元素a 转变成要的 元素b
3 //排序 sorted() sorted(Comparator<? super T> comparator);
4 //终止操作 1.匹配与查找 //allMatch 检查元素是否都符合条件 //anyMatch 是否有符合条件的 //noneMatch 是否有不符合条件的 //最大,和最小 max min 内部遍历 forEach
5 //映射和规约 map reduce 规约相当于总合 把所有的元素进行总合
6 //终止 1 收集 Collectors
package com.wkb.springmybatis.controller;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
@Test
public void test1(){
Stream<Employee> stream = EmployeeData.getEmployeeData().stream();
Stream<Employee> employeeStream = EmployeeData.getEmployeeData().parallelStream();
}
//筛选与切片 filter:过滤出符合条件的元素 limit:使其元素不超过一定值。 skip:跳过前面一定数量的元素 distinct :去除重复数据,要重写hashcode和equals方法
@Test
public void test2(){
List<Employee> employeeData = EmployeeData.getEmployeeData();
Stream<Employee> stream = employeeData.stream();
// Stream<Employee> employeeStream = EmployeeData.getEmployeeData().parallelStream();
//过滤age>30
// stream.filter(employee -> employee.getAge()>30).forEach(System.out::println);
//过滤name 模糊查询
// stream.filter(employee -> employee.getName().matches("^(.*马.*)$")).forEach(System.out::println);
String name = "马";
stream.filter(employee -> employee.getName().matches("^(.*"+name+".*)$")).forEach(System.out::println);
System.out.println("-----------------");
employeeData.stream().limit(3).forEach(System.out::println);
System.out.println("-----------------");
employeeData.stream().skip(3).forEach(System.out::println);
System.out.println("-----------------");
employeeData.add(new Employee(1001,"马化腾",34,6000.78));
employeeData.add(new Employee(1001,"马化腾",34,6000.78));
employeeData.stream().forEach(System.out::println);
System.out.println("-----------------");
employeeData.stream().distinct().forEach(System.out::println);
}
//映射 map :根据规则将元素a 转变成要的 元素b
@Test
public void test3(){
//1 map使用: 获取员工姓名长度大于3的员工姓名
List<Employee> employeeData = EmployeeData.getEmployeeData();
//map先获取姓名,filter过滤
employeeData.stream().map(Employee::getName).filter(str->str.length()>3).forEach(System.out::println);
//2 flatMap 传入的就是Stream<T> : map可能会Stream<Stream<String>> ,使用flatMap则 生成Stream<String> flatMap和Map的区别有点像List.add() 和 List.addAll()的区别
// Stream<R> rStream = employeeData.stream().flatMap(Employee::getName);
}
//排序 sorted() sorted(Comparator<? super T> comparator);
@Test
public void test4(){
List<Employee> employeeData = EmployeeData.getEmployeeData();
// 1 Employee需要实现 Comparable
employeeData.stream().sorted().forEach(System.out::println);
System.out.println("---------------");
// 2 传Comparator实现方法
employeeData.stream().sorted((e1,e2)->{return Double.compare(e1.getSalary(),e2.getSalary());}).forEach(System.out::println);
}
//终止操作 1.匹配与查找 //allMatch 检查元素是否都符合条件 //anyMatch 是否有符合条件的 //noneMatch 是否有不符合条件的 //最大,和最小 max min 内部遍历 forEach
@Test
public void test5(){
List<Employee> employeeData = EmployeeData.getEmployeeData();
//1. 匹配与查找
//allMatch 检查元素是否都符合条件
boolean b = employeeData.stream().allMatch(e -> e.getAge() > 20);
System.out.println(b);
System.out.println("---------------");
//anyMatch 是否有符合条件的
System.out.println(employeeData.stream().anyMatch(e->e.getSalary()>2000));
System.out.println("---------------");
//noneMatch 是否有不符合条件的
System.out.println(employeeData.stream().noneMatch(e->e.getSalary()>2000));
System.out.println("---------------");
System.out.println(employeeData.stream().count());
System.out.println("---------------");
System.out.println(employeeData.stream().findAny());
System.out.println("---------------");
System.out.println(employeeData.stream().findFirst());
System.out.println("---------------");
System.out.println("---------------");
//最大,和最小 max min
System.out.println(employeeData.stream().max(e->e.getSalary().intValue()));;
System.out.println(employeeData.stream().min((e1,e2)->{return Double.compare(e1.getSalary(),e2.getSalary());}));
// employeeData.stream().peek();
}
//映射和规约 map reduce 规约相当于总合 把所有的元素进行总合
@Test
public void test6(){
// 计算1-10的和
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Integer reduce = integers.stream().reduce(0, Integer::sum);
System.out.println(reduce);
//计算公司员工的所有工资总合
List<Employee> employeeData = EmployeeData.getEmployeeData();
Optional<Double> reduce1 = employeeData.stream().map(e -> e.getSalary()).reduce((e1, e2) -> e1 + e2);
System.out.println(reduce1.get());
}
//终止 1 收集 Collectors
@Test
public void test7(){
List<Employee> employeeData = EmployeeData.getEmployeeData();
employeeData.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.toList()).forEach(System.out::println);
System.out.println();
employeeData.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.toSet()).forEach(System.out::println);
employeeData.stream().filter(new Predicate(){
@Override
public boolean test(Object o) {
return false;
}
});
System.out.println();
Double collect = employeeData.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.averagingDouble(e -> e.getSalary()));
Double collec1t = employeeData.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.averagingDouble(new ToDoubleFunction<Employee>() {
@Override
public double applyAsDouble(Employee value) {
return value.getAge();
}
} ));
System.out.println(collect);
System.out.println(collec1t);
}
}