找做金融的网站有哪些,网站推广及seo方案,网络营销是什么意思?,产品展示网站方案转载自 JDK8新特性之函数式接口 什么是函数式接口
先来看看传统的创建线程是怎么写的
Thread t1 new Thread(new Runnable() {Overridepublic void run() {System.out.println(t1);}
});
t1.start();
再来看看使用了函数式接口是怎么写的
Thread t2 new Thre…转载自 JDK8新特性之函数式接口 什么是函数式接口
先来看看传统的创建线程是怎么写的
Thread t1 new Thread(new Runnable() {Overridepublic void run() {System.out.println(t1);}
});
t1.start();
再来看看使用了函数式接口是怎么写的
Thread t2 new Thread(() - System.out.println(t2));
t2.start();
Runnable接口直接可以使用Lambda表达式来编写这是因为Runnable接口是一个函数式接口来看看Runnable的源码。
FunctionalInterface
public interface Runnable {public abstract void run();}
发现该接口加上了函数式接口的定义注解 FunctionalInterface表明该接口是一个函数式接口。
Documented
Retention(RetentionPolicy.RUNTIME)
Target(ElementType.TYPE)
public interface FunctionalInterface {}
在JDK8中除了Runnbale接口还有像Comparator、Callable等接口都加上了该注解定义为函数式接口。
内置函数式接口
JDK8提供了几个内置的函数式接口用在了许多API的地方都可以拿来用可以满足大部分应用。
//ConsumerT - T作为输入执行某种动作但没有返回值
ConsumerString con (x) - {System.out.println(x);
};
con.accept(hello world);//SupplierT - 没有任何输入返回T
SupplierString supp () - {return Supplier;
};
System.out.println(supp.get());//PredicateT -T作为输入返回的boolean值作为输出
PredicateString pre (x) - {System.out.print(x);return x.startsWith(op);
};
System.out.println(: pre.test(op, hello World));// FunctionT, R -T作为输入返回的R作为输出
FunctionString, String function (x) - {System.out.print(x : );return Function;
};
System.out.println(function.apply(hello world));//BinaryOperatorT -两个T作为输入返回一个T作为输出对于“reduce”操作很有用
BinaryOperatorString bina (x, y) - {System.out.print(x y);return BinaryOperator;
};
System.out.println( bina.apply(hello , world));
自定义函数式接口
1、自定义一个函数式接口
FunctionalInterface
public interface CalcInterfaceN, V { V operation(N n1, N n2);
}
这里只有一个抽象方法FunctionalInterface注解可以不用写至于为什么可以往下看。
2、新建一个引用函数式接口的类
public static class NumberOperationN extends Number, V extends Number {private N n1;private N n2;public NumberOperation(N n1, N n2) {this.n1 n1;this.n2 n2;}public V calc(CalcInterfaceN, V ci) {V v ci.operation(n1, n2);return v;}}
3、测试函数式接口
private static void testOperationFnInterface() {NumberOperationInteger, Integer np new NumberOperation(13, 10);CalcInterfaceInteger, Integer addOper1 (n1, n2) - {return n1 n2;};CalcInterfaceInteger, Integer multiOper1 (n1, n2) - {return n1 * n2;};System.out.println(np.calc1(addOper1));System.out.println(np.calc1(multiOper1));// 上面的可以简写为System.out.println(np.calc1((n1, n2) - n1 n2));System.out.println(np.calc1((n1, n2) - n1 * n2));
}
最后输出
23
130
23
130
函数式接口规范
1、FunctionalInterface标识为一个函数式接口只能用在只有一个抽象方法的接口上。
2、接口中的静态方法、默认方法、覆盖了Object类的方法都不算抽象方法。
3、FunctionalInterface注解不是必须的如果该接口只有一个抽象方法可以不写它默认就符合函数式接口但建议都写上该注解编译器会检查该接口是否符合函数式接口的规范。
举例说明
正确的函数式接口。
FunctionalInterface
public interface CalcInterfaceN, V { V operation(N n1, N n2);
}
加了几个符合函数式的方法也没事编译器也不会报错。
FunctionalInterface
public interface CalcInterfaceN, V { V operation(N n1, N n2);public boolean equals(Object object);public default void defaultMethod() {}public static void staticMethod() {}
}
这个没用FunctionalInterface函数式接口有两个抽象方法不能用于Lambda表达式。
public interface CalcInterfaceN, V { V operation(N n1, N n2);V operation2(N n1, N n2);
}
这个有两个抽象方法的用FunctionalInterface注解的函数式接口编译会报错。
FunctionalInterface
public interface CalcInterfaceN, V { V operation(N n1, N n2);V operation2(N n1, N n2);
}
这个没有一个抽象方法编译报错。
public interface CalcInterfaceN, V {
}