示例代码:
- interface MyValue{
- double getValue();
- }
- interface MyParamValue{
- double getValue(double v);
- }
- interface NumericTest{
- boolean test(int n,int m);
- }
- public class lambdaDemo {
- public static void main(String[] args) {
- MyValue myVal=()->98.6; //lambda表达是符号
- System.out.println(myVal.getValue());
- MyParamValue myPVal=(n)->1/n;
- System.out.println(myPVal.getValue(4.0));
- System.out.println(myPVal.getValue(8.0));
- NumericTest isFactor=(n,d)->(n%d)==0;
- NumericTest lessThan=(n,m)->(n<m);
- NumericTest absEqual=(n,m)->(n<0?-n:n)==(m<0?-m:m);
- -----------------------------------------------------------------------------------
- 块lambda表达式:
- NumericFunc smallestF=(n)->{
- int result=1;
- n=n<0?-n:n;
- for(int i=2;i<=n/i;i++) {
- if(n%i==0) {
- result=i;
- break;
- }
- }
- return result;
- };
- System.out.println(smallestF.func(12));
- System.out.println(smallestF.func(11));
- }
- }
- ----------------------------------------------------------------------
- 泛型函数式接口:
- interface SomeTest<T>{
- boolean test(T n,T m);
- }
- SomeTest<Integer> isFactor=(n,d)->(n%d)==0;
- if(isFactor.test(10, 2)) {
- System.out.println("2 is a factor of 10.");
- }
- System.out.println();
- ---------------------------------------------------------------
- lambda表达式抛出异常
- import java.io.*;
- interface MyIOAction{
- boolean ioAction(Reader rdr)throws IOException; //抛出异常
- }
- interface MyTransform<T>{//使用数组作为形参
- void transform(T[] a);
- }
- public class lambdaExceptionDemo {
- public static void main(String[] args) {
- MyIOAction myIO=(rdr)->{
- int ch=rdr.read(); //可能抛出异常
- return true;
- };
- System.out.println("---------------------");
- Double[] values= {1.0,2.0,3.0,4.0};
- MyTransform<Double> sqrts=(v)->{
- for(int i=0;i<v.length;i++) {
- v[i]=Math.sqrt(v[i]);
- System.out.println(v[i]);
- }
- };
- sqrts.transform(values); //使用数组作为形参
- }
- }
- --------------------------------------------------------------------
- lambda表达式方法的引用 ClassName::MethdName
- interface IntPredicate{
- boolean test(int n);
- }
- class MyIntPredicates{
- private int v;
- MyIntPredicates(int x){
- v=x;
- }
- static boolean isPrime(int n) {
- if(n<2) {
- return false;
- }
- for(int i=2;i<=n/i;i++) {
- if(n%i==0) {
- return false;
- }
- }
- return true;
- }
- static boolean isEven(int n) {
- return (n%2)==0;
- }
- static boolean isPositive(int n) {
- return n>0;
- }
- boolean isFactor(int n){
- return (v%n)==0;
- }
- }
- public class MethodRefDemo {
- static boolean numTest(IntPredicate p,int v) {
- return p.test(v);
- }
- public static void main(String[] args) {
- boolean result;
- result=numTest(MyIntPredicates::isPrime,17);
- if(result) System.out.println("17 is Prime");
- result=numTest(MyIntPredicates::isEven,12);
- if(result) System.out.println("12 is Even");
- result=numTest(MyIntPredicates::isPositive,11);
- if(result) System.out.println("11 is Positive");
- MyIntPredicates myNum=new MyIntPredicates(12);
- IntPredicate ip=myNum::isFactor;
- result=ip.test(3);
- }
- }
- ------------------------------------------------------------------
- 构造函数的引用:ClassName::new
- interface MyFuncs{
- MyClass func(String s);
- }
- class MyClass{
- private String str;
- MyClass(String s){
- str=s;
- }
- MyClass(){
- str="";
- }
- String getStr() {
- return str;
- }
- }
- MyFuncs myClassCons=MyClass::new;
- MyClass mc=myClassCons.func("Test");
- System.out.println(mc.getStr());