Lambda表达式

1.Date可变,设计的很差,如setYear可改变其年,很差用
   jdk1.8:用LocalDate和LocalDateTime代替Date   
LocalDate localDate=LocalDate.now();
localDate.plusDays(1);
2.特色:
    a.函数式编程
    b.参数类型自动推断(可不指定参数类型)
    c.代码量少,简洁
3.好处:
    1.更简洁的代码
    2.更容易的并行
4.如何学:
    1.熟悉泛型,多用Stream Api
5.使用场景:
    1.任何有函数式(只有一个抽象方法的接口, Object的方法和default、static修饰的方法除外)接口的地方; @FunctionalInterface可加在函数式接口上
         
6.常见函数式接口:
    1.Supplier一个输出,获取结果;Function表明一个输入和一个输出(输入和输出通常不一样类型);UnaryOpeartory:一个输入和输出(二者同类型)
      
    2.Consumer表明一个输入;BiConsumer表明两个输入;BiFunction两输入,一输出(不一样类型);BinaryOpearator两输入,一输出(同类型)
     
     
    小结(基础):
         
    3.lambada语法:
      a.lambda表达式是对象(一个函数式接口的实例)
         
         
         
4.lambda:参数可省略(但要么全省或全不省);参数不能用final修饰;不能把lambda转给Object(非作的话,先转为函数式接口);
   不须要也不容许使用throws语句来声明它可能会抛出的异常
5.方法的引用:   直接访问类或者实例的已经存在的方法或者构造方法,提供了一种引用而不执行方法的方式,若是抽象方法的实现刚好
可使用调用另一个方法来实现,就有可能使用方法引用。
1.抽象方法没有输入参数,不能使用对象方法引用
构造方法引用:
Runnable r = () -> {};//无参无返回值
//有参无返回值
Consumer<String> c2 = (a) -> {};
Consumer<String> c3 = a -> {};
Consumer<String> c4 = (String a) -> {System.out.println(a);};
c4.accept("1");
//无参有返回值
Supplier<String> s1 = () -> "hello";
Supplier<String> s2 = () -> {return "hello";};
//有参有返回值
Function<String,Integer> f1=(str)->Integer.valueOf(str);
Function<String,Integer> f2=(String str)->Integer.valueOf(str);
Function<String,Integer> f3=(String str)->{return Integer.valueOf(str);};
System.out.println(f3.apply("2"));
6.Stream Api:处理数组和流的api
  Stream特色: a.不是数据结构,没有内部存储
    b.不支持索引访问,延迟计算
    c.支持并行,容易生成数组或集合(List、Set)
    d.支持过滤、查找、转换、汇总、聚合等操做
Stream运行机制:源Source  中间操做  终止操做
流的源能够是一个数组、集合、生成器方法、I/O通道等;一个流能够有零个或多个中间操做,
每个中间操做都会返回一个新的流,供下一个操做用;一个流只会有一个终止操做
Stream只有遇到终止操做,其源才开始执行遍历操做。
中间操做:过滤 filter
    去重 distinct   排序 sorted   截取 limit skip(忽略前多少个)
    转换 map/flatMap    其余peek(经常使用语输出信息)
终止操做:
  循环 forEach  计算:min、max、count、average
  匹配:anyMatch、allMatch、noneMatch、findFirst、findAny
  汇聚:reduce   收集器:toArray collect
Stream的建立:
   1.经过数组 Stream.of(arr)  2.经过集合   3.经过Stream.generate方法来建立
   4.经过Stream.iterate方法    5.其余Api来建立String a="aaa";IntStream intStream=a.chars();
Stream<Integer> stream=Stream.iterate(1,x->x+1);
stream.limit(10).forEach(System.out::println);
Stream<Integer> stream=Stream.generate(()->1);
stream.limit(10).forEach(System.out::println);
List<Integer> list = Stream.iterate(1, x -> x + 1).limit(50).filter(x -> x % 2 == 0).collect(Collectors.toList());//搜集50前的偶数为一个list
String s="11,22,33";
List<Integer> list = Stream.of(s.split(",")).map(Integer::valueOf).collect(Collectors.toList());//搜集成list
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","6");//设置并发线程数6
Optional<Integer> result = Stream.iterate(1, x -> x + 1).limit(200).parallel().peek(x -> {
System.out.println(Thread.currentThread().getName());//并行计算    
}).max(Integer::compare);
sequential()//串行计算
String indexStr = "itemId=1&userId=2&type=2&token=3333&key=index";   //拆分转换成map
Map<String, String> param = Stream.of(indexStr.split("&")).map(s -> s.split("=")).collect(Collectors.toMap(s -> s[0], s -> s[1]));
Collectors.joining(",","(",")");//(1,2) 形式输出
分组统计:






























































相关文章
相关标签/搜索