| 一、四大内置函数式接口public class Class001_Functional {
 ? ?public static void main(String[] args) {
 ? ? ? ?testComsumer(2000,m-> System.out.println("每天固定给女主播刷超级火箭"+m));
 ? ? ? ?testComsumer(500,m-> System.out.println("每天做脸消费"+m));
?
 ? ? ? ?System.out.println(strHandler("nihaoa ? ",s-> s.trim()));;
 ? ? ? ?System.out.println(strHandler("nihaoa ? ",s-> s.toUpperCase()));;
?
 ? ? ? ?System.out.println(testSupplier(5,()->(int)(Math.random()*(5-1+1)+1)));
 ? ? ? ?System.out.println(testSupplier(46,()->(int)(Math.random()*(20-10+1)+10)));
?
 ? ? ? ?System.out.println(testPredicate(List.of("abc","abcd","abcde"),s->s.length()>3));
 ?  }
?
 ? ?//对集合中的多个字符串进行某种规则的过滤,返回满足条件的字符串
 ? ?public static List<String> testPredicate(List<String> list, Predicate<String> pre){
 ? ? ? ?List<String> newList = new ArrayList<>(); //存放满足条件的字符串
 ? ? ? ?for(String str:list){
 ? ? ? ? ? ?if(pre.test(str)){
 ? ? ? ? ? ? ? ?newList.add(str);
 ? ? ? ? ?  }
 ? ? ?  }
 ? ? ? ?return newList;
 ?  }
?
 ? ?//生成指定个数的不同规则的整数
 ? ?public static List<Integer> testSupplier(int num, Supplier<Integer> sup){
 ? ? ? ?List<Integer> list = new ArrayList<>(); //存放生成的所有整数
 ? ? ? ?for(int i=1;i<=num;i++){
 ? ? ? ? ? ?list.add(sup.get());
 ? ? ?  }
 ? ? ? ?return list;
 ?  }
?
 ? ?//对字符串进行某种行为处理,返回结果
 ? ?public static String strHandler(String str, Function<String,String> fun){
 ? ? ? ?return fun.apply(str);
 ?  }
?
 ? ?//每天固定消费某些金额做某些行为
 ? ?public static void testComsumer(double money, Consumer<Double> com){
 ? ? ? ?com.accept(money);
 ?  }
}二、方法引用方法引用:: 简化lambda表达式 是lambda表达式的另外一种表现形式 使用前提: ? 当lambda表达式的lambda体中的实现已经存在另外一个方法实现了,只需要在lambda体中调用另一个方法--->可以通过方法引用的写法直接引用那个方法 ? lambda表达式重写的抽象方法的参数列表与返回值对应内部所引用方法的参数列表与返回值 ? lambda返回值与内部所调用方法的方法直接对应,lambda的参数列表的第一个参数作为内部调用成员方法的对象存在,lambda的第二到多个参数直接匹配内部所调用方法的参数列表存在 分类 ? 对象::成员方法 ? 要求: lambda表达式重写的抽象方法的参数列表与返回值对应内部所引用方法的参数列表与返回值 ? 类名::静态方法 ? lambda表达式重写的抽象方法的参数列表与返回值对应内部所引用方法的参数列表与返回值 ? 类名::成员方法 ? lambda返回值与内部所调用方法的方法直接对应,lambda的参数列表的第一个参数作为内部调用成员方法的对象存在,lambda的第二到多个参数直接匹配内部所调用方法的参数列表存在 public class Class002_Quite {
 ? ?public static void main(String[] args) {
 ? ? ? ?//对象::成员方法
 ? ? ? ?PrintStream ps = ?System.out;
 ? ? ? ?Consumer<Integer> com = i-> ps.println(i);
 ? ? ? ?//考虑是否可以通过方法引用简化 : 1)lambda体的实现是否是通过调用另外一个方法实现的--> √ println
 ? ? ? ?// 2)考虑Consumer中的抽象方法的参数列表与返回值是否对应内部引用println方法的参数列表与返回值--> √
 ? ? ? ?com = ps::println;
 ? ? ? ?com = System.out::println;
 ? ? ? ?//List.of(1,2,3,4,5).forEach(com);
 ? ? ? ?List.of(1,2,3,4,5).forEach(System.out::println);
?
 ? ? ? ?//类名::静态方法
 ? ? ? ?//求两个小数中的最大值
 ? ? ? ?BiFunction<Double,Double,Double> fun = (x,y)->Math.max(x,y);
 ? ? ? ?//分析是否可以通过方法引用简化: 1)lambda体的实现是否是通过调用另外一个方法实现的--> √ max
 ? ? ? ?// 2)apply方法的参数列表与返回值是否直接匹配内部所调用方法的参数列表与返回值 -> √
 ? ? ? ?fun = Math::max;
 ? ? ? ?System.out.println(fun.apply(2.1,1.8));;
?
 ? ? ? ?//类名::成员方法
 ? ? ? ?BiPredicate<String,String> pre = (x,y)->x.equals(y);
 ? ? ? ?//分析是否可以通过方法引用简化:  1)lambda体的实现是否是通过调用另外一个方法实现的-->√  equals
 ? ? ? ?// 2)lambda返回值与内部所调用方法的方法直接对应,lambda的参数列表的第一个参数作为内部调用成员方法的对象存在,lambda的第二到多个参数直接匹配内部所调用方法的参数列表存在 --> √  可以使用类名::成员方法名简化
 ? ? ? ?pre = String::equals;
 ? ? ? ?System.out.println(pre.test("nihao","nihao"));;
 ?  }
} 三、StreamStream : 关注流式运算,不是传输与存储 io流关注数据的传输 集合与数组关注数据的存储 注意: ? 1.Stream是一次性的流,一旦使用过就不能重复使用 ? 2.Stream的操作不会影响数据源中的数据 ? 3.延迟执行|惰性加载: 当获取终止行为时候才会执行中间操作 使用步骤: ? 1.获取Stream流 ? 2.中间行为 : 一系列的流式操作 ? 3.终止行为 3.1 创建Stream 创建Stream的方式: 1.Collection Stream<E> stream() 2.Arrays stream() 3.Stream.of(值列表) public class Class001_Stream {
 ? ?public static void main(String[] args) {
 ? ? ? ?//1..Collection  Stream<E> stream()
 ? ? ? ?List<Integer> list = List.of(1,2,3,4,5);
 ? ? ? ?Stream<Integer> stream = list.stream();
 ? ? ? ?stream.forEach(System.out::println);
?
 ? ? ? ?//2.Arrays  stream()
 ? ? ? ?String[] arr = {"abc","bbb","ccc"};
 ? ? ? ?Stream<String> stream1 = Arrays.stream(arr);
 ? ? ? ?stream1.forEach(System.out::println);
?
 ? ? ? ?//3.Stream.of(值列表)
 ? ? ? ?Stream<Integer> stream2 = Stream.of(3,2,1);
 ? ? ? ?stream2.forEach(System.out::println);
 ?  }
}3.2 Stream的中间行为 流式中间操作 1.过滤 Stream<T> filter(Predicate<? super T> predicate); 2.去重 Stream<T> distinct() 3.截取 Stream<T> limit(long maxSize) 4.跳过 Stream<T> skip(long) 5.排序 sorted() | sorted(Comparator<? super T> comparator) 6.映射 map(Function) :接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素 注意:中间操作都会返回一个持有结果新的流 public class Class002_Stream {
 ? ?static List<Employee> list = Arrays.asList(
 ? ? ? ? ? ?new Employee("lisi",19,5000),
 ? ? ? ? ? ?new Employee("wangwu",18,4000),
 ? ? ? ? ? ?new Employee("tianqi",17,4000),
 ? ? ? ? ? ?new Employee("zhaoliu",20,3000),
 ? ? ? ? ? ?new Employee("zhangsan",20,6000),
 ? ? ? ? ? ?new Employee("zhangsan",20,6000)
 ?  );
 ? ?public static void main(String[] args) {
 ? ? ? ?//1.获取一个stream
 ? ? ? ?Stream<Employee> stream = list.stream();
 ? ? ? ?//2.中间操作 延迟执行
 ? ? ? ?/*stream = stream.filter(e->{
 ? ? ? ? ? ?System.out.println("--------------过滤-----------");
 ? ? ? ? ? ?return e.getAge()>=18;
 ? ? ? ?}).distinct()
 ? ? ? ?.limit(3)
 ? ? ? ?.skip(1)
 ? ? ? ?.filter(e->{
 ? ? ? ? ? ?System.out.println("-----------最终过滤------------"+e);
 ? ? ? ? ? ?return true;
 ? ? ? ?});*/
?
 ? ? ? ?//排序
 ? ? ? ?//stream = list.stream().sorted();
 ? ? ? ?// stream = list.stream().sorted((x,y)->Double.compare(y.getSalary(),x.getSalary()));
?
 ? ? ? ?//映射
 ? ? ? ?//Stream<Double> stream2 = list.stream().map(e->e.getSalary());
 ? ? ? ?Stream<Double> stream2 = list.stream().map(Employee::getSalary);
?
 ? ? ? ?//3.终止行为
 ? ? ? ?stream2.forEach(System.out::println);
 ?  }
}3.3 Stream 流的终止行为 规约 map-reduce 先加工->后处理汇总结果 收集(collect) collect 主要依赖 java.util.stream.Collectors 类内置的静态方法。 Collectors.toList() public class Class003_Stream {
 ? ?static List<Employee> list = Arrays.asList(
 ? ? ? ? ? ?new Employee("lisi",19,5000),
 ? ? ? ? ? ?new Employee("wangwu",18,4000),
 ? ? ? ? ? ?new Employee("tianqi",17,4000),
 ? ? ? ? ? ?new Employee("zhaoliu",20,3000),
 ? ? ? ? ? ?new Employee("zhangsan",20,6000),
 ? ? ? ? ? ?new Employee("zhangsan",20,6000)
 ?  );
 ? ?public static void main(String[] args) {
 ? ? ? ?//1.获取一个stream
 ? ? ? ?Stream<Employee> stream = list.stream();
 ? ? ? ?//2.中间操作,终止行为
 ? ? ? ?//anyMatch-检查是否至少匹配一个元素
 ? ? ? ?System.out.println( stream.allMatch(e->e.getAge()>=18));;
 ? ? ? ?//需求:按工资排序,获取第一个员工信息
 ? ? ? ?// findFirst-返回第一个元素
 ? ? ? ?//1)降序排序  2)找到第一个元素
 ? ? ? ?Optional<Employee> op1= ?list.stream().sorted((x, y)->Double.compare(y.getSalary(),x.getSalary()))
 ? ? ? ? ? ? ?  .findFirst();
 ? ? ? ?if(op1.isPresent()){
 ? ? ? ? ? ?System.out.println(op1.get());
 ? ? ?  }
 ? ? ? ?//并行流
 ? ? ? ?op1 = list.parallelStream().findAny();
 ? ? ? ?System.out.println(op1.get());
?
 ? ? ? ?//薪资>5000的员工个数
 ? ? ? ?// count-返回流中元素的总个数
 ? ? ? ?System.out.println(list.stream().filter(e->e.getSalary()>5000).count());
?
 ? ? ? ? max-返回流中最大值
 ? ? ? ? min-返回流中最小值
 ? ? ? ?System.out.println(list.stream().max((x,y)->Double.compare(x.getSalary(),y.getSalary())).get());
?
 ? ? ? ?//规约 map-reduce  先加工->后处理汇总结果
 ? ? ? ?System.out.println(list.stream().map(Employee::getSalary).reduce((x,y)->x+y).get());;
?
 ? ? ? ?System.out.println(Stream.of(1,2,3,4,5).reduce((x,y)->{
 ? ? ? ? ? ?System.out.println("========>x = "+ x + " , y ="+y );
 ? ? ? ? ? ?return x+y;
 ? ? ?  }).get());;
?
 ? ? ? ?System.out.println(Stream.of(1,2,3,4,5).reduce(100,(x,y)->{
 ? ? ? ? ? ?System.out.println("========>x = "+ x + " , y ="+y );
 ? ? ? ? ? ?return x+y;
 ? ? ?  }));
?
 ? ? ? ?// 收集(collect)
 ? ? ? ?//collect 主要依赖 java.util.stream.Collectors 类内置的静态方法。
 ? ? ? ?//Collectors.toList()
 ? ? ? ?List<String> names = list.stream().distinct().map(Employee::getName).collect(Collectors.toList());
 ? ? ? ?System.out.println(names);
?
 ? ? ? ?Set<String> names2 = list.stream().map(Employee::getName).collect(Collectors.toSet());
 ? ? ? ?System.out.println();
?
 ? ? ? ?Map<String,Double> map = list.stream().distinct().collect(Collectors.toMap(Employee::getName,Employee::getSalary));
 ? ? ? ?System.out.println(map);
 ?  }
} |