12个优秀平面设计素材网站,餐饮设计网站建设,高端大气网页,wordpress后台能登陆前台却不行Java8新特性1——函数式接口lambda表达式 注#xff1a;以下内容基于Java 8#xff0c;所有代码都已在Java 8环境下测试通过 目录#xff1a;
Java8新特性1——函数式接口lambda表达式方法引用Stream
1. 函数式接口
如果在一个接口中#xff0c;有且只有一个抽…Java8新特性1——函数式接口lambda表达式 注以下内容基于Java 8所有代码都已在Java 8环境下测试通过 目录
Java8新特性1——函数式接口lambda表达式方法引用Stream
1. 函数式接口
如果在一个接口中有且只有一个抽象方法则该接口被称为函数式接口。如
interface Test {void test();
}注
可以在接口前使用 FunctionalInterface 注解判断这个接口是否是⼀个函数式接口。如
FunctionalInterface
interface Test1 {//有且仅有一个抽象方法是函数式接口void test();
}FunctionalInterface
interface Test2 {//有且仅有一个抽象方法是函数式接口void test();default void f() {}
}FunctionalInterface
interface Test3 {//没有抽象方法不是函数式接口编译器报错
}FunctionalInterface
interface Test4 {//有多个抽象方法不是函数式接口编译器报错void test1();void test2();
}2. lambda表达式
2.1 lambda表达式作用
lambda表达式是一个匿名函数用于简化函数式接口的实现。
在Java中接口不能实例化但接口对象可以指向实现类对象。当没有实现类对象时可以通过匿名类的方式如
public class Main {public static void main(String[] args) {Test test new Test() {Overridepublic void f() {System.out.println(使用匿名函数的方式实现了函数式接口);}};test.f();}
}FunctionalInterface
interface Test {void f();
}使用匿名类的方式代码不是很简洁因此引入了lambda表达式如
public class Main {public static void main(String[] args) {Test test () - System.out.println(使用lambda表达式的方式实现了函数式接口);test.f();}
}FunctionalInterface
interface Test {void f();
}在使用lambda表达式之后代码变得简洁了很多因此可以说lambda表达式是和函数式接口相辅相成的。在上面的代码中lambda表达式实际做了以下三个工作 自动实现接口 Test test new Test();将 - 前的参数自动添加到抽象函数里面上面代码中抽象函数没有参数 void f();将 - 后的语句作为抽象函数的方法体 void f(){System.out.println(使用lambda表达式的方式实现了函数式接口);
}2.2 lambda表达式语法格式
lambda表达式的格式如下
(参数1, 参数2, ……) - {方法体;
}其中
参数要求和函数式接口中抽象方法的参数一致包括数量和类型以及顺序如果函数式接口中抽象方法有返回值则实现的时候也需要返回值
public class Main {public static void main(String[] args) {Test test (int x, int y) - {//参数、返回值与函数式接口中抽象方法一致return x y;};test.add(1, 2);}
}FunctionalInterface
interface Test {int add(int x, int y);
}2.3 lambda表达式的精简 参数精简 参数类型可以省略若省略一个类型参数则所有的类型参数都要省略若只有一个参数则小括号可以省略若参数为0或者多于1个则小括号不可以省略 方法体精简 若方法体中只有一行代码则花括号可以省略若方法体中只有一行代码且是return语句则在省略大括号的时候还需要去掉return关键字若方法体中有多行代码或者使用了return语句则大括号不可以省略 public class Main {public static void main(String[] args) {//只有一个参数省略了小括号//只有一条return语句省略了花括号即return关键字Test test x - Math.exp(x);test.exp(1);}
}FunctionalInterface
interface Test {double exp(double x);
}2.4 变量作用域 lambda表达式只可以访问外部变量但不能修改外部变量lambda表达式访问的外部变量一般都是声明为 final 的但也可以不用声明为 final 但该变量在声明后不能被修改lambda表达式中不允许声明一个与局部变量同名的参数或局部变量 public class Main {static final int a 0;public static void main(String[] args) {final int num1 10;int num2 20;//num2 40; //声明后不能被修改Test test1 x - {System.out.println(num1);//可以访问外部被声明为 final 的变量System.out.println(num2);//可以访问外部的普通变量//num1 20;//只能访问不能修改//num2 20;//只能访问不能修改//int num1 20;//不允许声明一个与局部变量同名的局部变量return Math.exp(x);};//num2 40; //声明后不能被修改test1.exp(1);//不允许声明一个与局部变量同名的参数//Test test2 num1 - Math.exp(num1);}
}FunctionalInterface
interface Test {double exp(double x);
}3. 四大函数式接口
为了让开发者高效地使用函数式接口Java 8 在 java.util.function 包下提供了许多函数式接口以下四种是最为常见的
接口原型抽象方法备注Consumer T accept(T t)消费型接口Supplier T T get()供给型接口FunctionT, RR apply(T t)函数型接口Predicate T boolean test(T t)断言型接口
3.1 Consumer T 消费型接口
该接口只接收输入参数但不输出返回值消费对象只进不出。 接口原型 FunctionalInterface
public interface ConsumerT {void accept(T t);
}使用示例 import java.util.function.Consumer;public class Main {public static void main(String[] args) {ConsumerInteger acc (t) - System.out.println(t);//实现 Consumer 接口acc.accept(10);}
}3.2 Supplier T 供给型接口
该接口只输出返回值但不接收输入参数生成对象只出不进。 接口原型 public interface SupplierT {T get();
}使用示例 import java.util.function.Supplier;public class Main {public static void main(String[] args) {SupplierInteger sup () - 10;//实现 Supplier 接口System.out.println(sup.get());}
}3.3 FunctionT, R函数型接口
该接口既接收输入参数又输出返回值用于指定特定功能有进有出。 接口原型 FunctionalInterface
public interface FunctionT, R {R apply(T t);
}使用示例 import java.util.function.Function;public class Main {public static void main(String[] args) {FunctionInteger, String fun (x) - {//实现 Function 接口String out 输入的整数是 x;return out;};System.out.println(fun.apply(10));}
}3.4 Predicate T 断言型接口
该接口既接收输入参数又输出返回值且返回值只能是布尔值用于条件判断有进有出。 函数原型 public interface PredicateT {boolean test(T t);
}使用示例 import java.util.function.Predicate;public class Main {public static void main(String[] args) {PredicateInteger pre (x) - x % 2 0;//实现 Predicate 接口int a 10;if (pre.test(10)) {System.out.println(a 是偶数);} else {System.out.println(a 是奇数);}}
} 文章转载自: http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.kghss.cn.gov.cn.kghss.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.jhrkm.cn.gov.cn.jhrkm.cn http://www.morning.pmnn.cn.gov.cn.pmnn.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.lzsxp.cn.gov.cn.lzsxp.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.wylpy.cn.gov.cn.wylpy.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.jntcr.cn.gov.cn.jntcr.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.rfjmy.cn.gov.cn.rfjmy.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.hkshy.cn.gov.cn.hkshy.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.zmbzl.cn.gov.cn.zmbzl.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.hytfz.cn.gov.cn.hytfz.cn http://www.morning.fwcjy.cn.gov.cn.fwcjy.cn http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.yrycb.cn.gov.cn.yrycb.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.rtspr.cn.gov.cn.rtspr.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.bxqry.cn.gov.cn.bxqry.cn http://www.morning.npxcc.cn.gov.cn.npxcc.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.rqkk.cn.gov.cn.rqkk.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.ngdkn.cn.gov.cn.ngdkn.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.xtlty.cn.gov.cn.xtlty.cn