您好,欢迎访问三七文档
Java8部分新特性1Lambda表达式特性2流处理3接口增强Lambda表达式PART1传统写法与Lambda写法对比自定义接口:@FunctionalInterfaceinterfaceConverterF,T{Tconvert(Ffrom);}传统写法:ConverterString,Integerconverter=newConverterString,Integer(){@OverridepublicIntegerconvert(Stringfrom){returnInteger.valueOf(from);}};Integerresult=converter.convert(200);System.out.println(result);传统写法与Lambda写法对比Lambda写法:ConverterString,Integerconverter=(param)-Integer.valueOf(param);Integerresult=converter.convert(101);System.out.println(result);同理:传统写法:newThread(newRunnable(){@Overridepublicvoidrun(){System.out.println(hellolambda);}}).start();Lambda写法:newThread(()-System.out.println(hellolambda)).start();Lambda表达式010203Lambda表达式又被成为闭包或者匿名方法。Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中)。使用Lambda表达式可以使代码变的更加简洁紧凑。示例:(inta,intb)-a+b;(a,b)-a-b;(inta,intb)-{returna*b;};Lambda表达式语法格式如下:(parameters)-expression或者(parameters)-{statements;}parameters:类似方法中的形参列表,这里的参数是函数式接口里的参数。这里的参数类型可以明确的声明也可不声明而由JVM隐含的推断。另外当只有一个推断类型时可以省略掉圆括号。-:可理解为“被用于”的意思。方法体:可以是表达式也可以代码块,是函数式接口里方法的实现。代码块可返回一个值或者什么都不返回,这里的代码块块等同于方法的方法体。如果是表达式,也可以返回一个值或者什么都不返回。Lamda表达式更多示例://示例1:不需要接受参数,直接返回10()-10;//示例2:接受两个int类型的参数,并返回这两个参数相加的和(intx,inty)-x+y;//示例3:接受x,y两个参数,该参数的类型由JVM根据上下文推断出来,并返回两个参数的和(x,y)-x+y;//示例4:接受一个字符串,并将该字符串打印到控制到,不返回结果(Stringname)-System.out.println(name);//示例5:接受一个推断类型的参数name,并将该字符串打印到控制台name-System.out.println(name);//示例6:接受两个String类型参数,并分别输出,不返回(Stringname,Stringsex)-{System.out.println(name);System.out.println(sex);};//示例7:接受一个参数x,并返回该该参数的两倍x-2*x;Lamda表达式010203方法引用通过方法的名字来指向一个方法。方法引用可以使语言的构造更紧凑简洁,减少冗余代码。方法引用使用一对冒号::。构造器引用:它的语法是Class::new,或者更一般的ClassT::new。静态方法引用:它的语法是Class::static_method。特定类的任意对象的方法引用:它的语法是Class::method。特定对象的方法引用:它的语法是instance::method。//构造器引用SupplierApplesupplier=Apple::new;Appleapple=supplier.get();//构造器引用FunctionInteger,Applefunction=Apple::new;Appleapple1=function.apply(10);apple1.setColor(green);apple1.setPrice(2.00);ListAppleappleList=Arrays.asList(apple,apple1);//静态方法引用appleList.forEach(Apple::printPrice);//特定类的任意对象的方法引用appleList.forEach(Apple::printColor);//特定对象的方法引用appleList.forEach(apple1::add);方法引用Lamda表达式完整示例/***基础类*/@Getter@SetterpublicclassApple{privateStringcolor;privateIntegernumber;privatedoubleprice;publicApple(){color=yellow;number=20;price=10.00;}publicApple(Integernumber){this.number=number;}publicstaticvoidprintPrice(finalAppleapple){System.out.println(价格为:+apple.getPrice());}publicvoidprintColor(){System.out.println(颜色为:+color);}publicvoidadd(finalAppleapple){System.out.println(每一个都加上当前的数目为:+(number+apple.getNumber()));}}Lamda表达式完整示例publicclassTest{publicstaticvoidmain(String[]args){//构造器引用SupplierApplesupplier=Apple::new;Appleapple=supplier.get();//构造器引用FunctionInteger,Applefunction=Apple::new;Appleapple1=function.apply(10);apple1.setColor(green);apple1.setPrice(2.00);ListAppleappleList=Arrays.asList(apple,apple1);//静态方法引用appleList.forEach(Apple::printPrice);//特定类的任意对象的方法引用appleList.forEach(Apple::printColor);//特定对象的方法引用appleList.forEach(apple1::add);}}运行结果Lamda表达式010203函数式接口,@FunctionalInterface,简称FI,简单的说,FI就是指仅含有一个抽象方法的接口,以@Functionalnterface标注,注意,这里的抽象方法指的是该接口自己特有的抽象方法,而不包含它从其上级继承过来的抽象方法@FunctionalInterfaceinterfaceConverterF,T{Tconvert(Ffrom);}内置的常用接口类型:PredicateT——接收T对象并返回booleanConsumerT——接收T对象,不返回值FunctionT,R——接收T对象,返回R对象SupplierT——提供T对象(例如工厂),不接收值BinaryOperatorT——接收两个T对象,返回一个T对象UnaryOperatorT——接收一个T对象,返回一个T对象函数式接口Lamda表达式实战//BinaryOperatorBinaryOperatorIntegeroperator=(a,b)-a+b;System.out.println(operator.apply(1,2));//UnaryOperatorUnaryOperatorIntegerunaryOperator=a-a*2;System.out.println(unaryOperator.apply(4));//ConsumerConsumerStringconsumer=System.out::println;consumer.accept(helloworld);//FunctionFunctionString,Integerfunction=Integer::parseInt;System.out.println(function.apply(100));//SupplierSupplierStringsupplier=()-你好;System.out.println(supplier.get());//PredicatePredicateIntegerpredicate=a-a10;System.out.println(predicate.test(2));流处理PART2流处理举个栗子:传统写法:ListAppleappleList=Arrays.asList(apple,apple1,apple2);ListApplechooseAppleList=newArrayList();for(Appletemp:appleList){if(temp.getPrice()3.00){chooseAppleList.add(temp);}}//顺序ComparatorApplecomparator=newComparatorApple(){@Overridepublicintcompare(Appleo1,Appleo2){returno2.getColor().compareTo(o1.getColor());}};chooseAppleList.sort(comparator);流处理举个栗子:流处理写法:appleList.stream().filter(apple-apple.getPrice()3).sorted(Comparator.comparing(Apple::getColor)).collect(Collectors.toList());并行流写法:appleList.parallelStream().filter(apple-apple.getPrice()3).sorted(Comparator.comparing(Apple::getColor)).collect(Collectors.toList());流处理流水线处理Applefiltersortedcollectlamdalamda…..声明行-更简洁,更易读可复合-更灵活可并行-性能更易优化流处理流操作中间操作(短路,循环合并等优化)操作操作函数函数描述符FilterPredicateTT-booleanMapFunctionT,RT-RDistinctFunction?superT,?extendsStream?extendsRT-RLimitSortedComparatorT(T,T)-int…终端操作:操作目的函数函数描述符forEach消费流中的每一个元素应用lambdaconsumeTT-voidcount返回流中元素个数Collect规约流成集合,返回List,Map等findAny查找复合的任一元素PredicateTT-boolean…流处理常用流操作:操作类型返回类型函数式接口函数描述符filter中间S
本文标题:Java8-新特性
链接地址:https://www.777doc.com/doc-5550242 .html