一个接口有且只有一个抽象方法,(Object的public方法除外)那么该接口就是一个函数式接口。
有FunctionalInterface注解,那么编译器就会认定为函数式接口。
java.lang.Runnable
java.util.concurrent.Callablejava.security.PrivilegedAction
java.util.Comparatorjava.io.FileFilter
java.nio.file.PathMatcher
java.lang.reflect.InvocationHandlerjava.beans.PropertyChangeListener
java.awt.event.ActionListenerjavax.swing.event.ChangeListener
JDK1.8新增:
3.函数式接口解析
BiConsumer:
BiConsumer
其中还有一个andThen方法,该方法接受一个BiConsumer,返回一个组合的BiConsumer,并且按照顺序执行操作。如果执行任一操作抛出异常,则将其传递给组合操作的调用者。 如果执行此操作抛出异常,将不执行后操作(after)。
public static void main(String[] args) {
System.out.println("------BiConsumer------");
BiConsumer<String,String> biConsumer = (x,y)-> System.out.println(x+"--------------"+y);
BiConsumer<String,String> biConsumer2 = (x,y)-> System.out.println(x+">>>>>>>>"+y);
biConsumer.accept("JACK","JAVA8");
biConsumer.andThen(biConsumer2).accept("biConsumer2","JAVA8");
System.out.println("------BiConsumer------");
}
输出内容为:
------BiConsumer------
JACK--------------JAVA8
biConsumer2--------------JAVA8
biConsumer2>>>>>>>>JAVA8
------BiConsumer------
BiFunction:
BiFunctionandThen(Function super R, ? extends V> after)
,接受一个Function方法,返回一个结果。
public static void main(String[] args) {
System.out.println("------BiFunction------");
BiFunction<Integer,Integer,Integer> = (x, y)-> {return x+y;};
int result = biFunction.apply(1,2);
System.out.println("result:"+result);
Function<Integer,Integer> function = (x)->{return (x*5);};
System.out.println("andThen:"+biFunction.andThen(function).apply(8,9));
System.out.println("------BiFunction------");
}
------BiFunction------
result:3
andThen:85
------BiFunction------
上面的result:3是很容易理解的,就是1+2,也就是执行了biFunction
,下面输出的85,则是先执行了biFunction.apply(8,9)
,然后在执行的自定义的function
函数。最后计算公式也就是(8+9*5=85。
BinaryOperator:
代表了一个作用于于两个同类型操作符的操作,并且返回了操作符同类型的结果。他继承了BiFunction,因此可以使用BiFunction中的方法。
public class MainTest {
public static void main(String[] args) {
System.out.println("------BinaryOperator------");
BinaryOperator<Integer> binaryOperator = (x,y)->x*y;
System.out.println("BinaryOperator:"+binaryOperator.apply(3,8));
Function<Integer,Integer> function1 = (x)->x*8;
System.out.println("BinaryOperator andThen:" + binaryOperator.andThen(function1).apply(8,10));
BinaryOperator<Integer> bi = BinaryOperator.maxBy(Comparator.naturalOrder());
System.out.println("BinaryOperator maxBy:" + (bi.apply(100,99)));
System.out.println("------BinaryOperator------");
}
}
输出结果:
------BinaryOperator------
BinaryOperator:24
BinaryOperator andThen:640
BinaryOperator maxBy:100
------BinaryOperator------
BiPredicate:
代表了一个两个参数的boolean值方法。该接口是一个逻辑运算的函数接口;里面包含了比较方法boolean test(T t, U u)
,逻辑与方法and(BiPredicate super T, ? super U> other)
,逻辑非方法negate()
,以及逻辑或方法or(BiPredicate super T, ? super U> other)
。
test(T t, U u)
:判断参数是否满足条件。
and(BiPredicate super T, ? super U> other)
:同时满足。
negate()
:非运算,类似“!”.
or(BiPredicate super T, ? super U> other)
:或运算。
public class MainTest {
public static void main(String[] args) {
System.out.println("------BiPredicate------");
BiPredicate<String,String> biPredicate = (x,y)-> x.equals(y);
BiPredicate<String,String> biPredicate1 = (x,y)-> (x+"2").equals(y);
System.out.println("False>>>>>:"+biPredicate.test("a","b"));
System.out.println("True>>>>>:"+biPredicate.test("a","a"));
//biPredicate和biPredicate1是否同时满足
System.out.println("and >>>>>:"+biPredicate.and(biPredicate1).test("xxx","xxx2"));
//negate表示非运算,类似"!"
System.out.println("negate >>>>>:"+biPredicate.negate().test("a","a"));
System.out.println("negate >>>>>:"+biPredicate.negate().test("a","c"));
//or或者
System.out.println("or >>>>>:"+biPredicate.or(biPredicate1).test("xxx","xxx2"));
System.out.println("or >>>>>:"+biPredicate.or(biPredicate1).test("xxx","xx"));
System.out.println("------BiPredicate------");
}
}
输出结果:
------BiPredicate------
False>>>>>:false
True>>>>>:true
and >>>>>:false
negate >>>>>:false
negate >>>>>:true
or >>>>>:true
or >>>>>:false
------BiPredicate------
BooleanSupplier:
代表了boolean值结果的提供方,用于接收Lambda表达式所返回的boolean值结果。
public class MainTest {
public static void main(String[] args) {
System.out.println("------BooleanSupplier------");
BooleanSupplier booleanSupplier = ()->true;
System.out.println(" booleanSupplier :" + booleanSupplier.getAsBoolean());
int x=2;
int y=3;
BooleanSupplier booleanSupplier1 = ()->x>y;
System.out.println(" booleanSupplier1 :" + booleanSupplier1.getAsBoolean());
System.out.println("------BooleanSupplier------");
}
}
输出结果:
------BooleanSupplier------
booleanSupplier :true
booleanSupplier1 :false
------BooleanSupplier------
Consumer:
代表了接受一个输入参数并且无返回的操作,给定一个参数,对其进行消费处理,处理方式可以是任意操作。
public class MainTest {
public static void main(String[] args) {
System.out.println("------Consumer------");
Consumer consumer = (m)-> System.out.println(">>>>>"+m);
consumer.accept("这是第一个consumer");
consumer.andThen(consumer).accept("This is second");
System.out.println("------Consumer------");
}
}
输出内容:
------Consumer------
>>>>>这是第一个consumer
>>>>>This is second
>>>>>This is second
------Consumer------
DoubleBinaryOperator:
代表了作用于两个double值操作符的操作,并且返回了一个double值的结果。
public class MainTest {
public static void main(String[] args) {
System.out.println("------DoubleBinaryOperator------");
DoubleBinaryOperator doubleBinaryOperator = (a,b)->{
if(a>b){
return a-b;
}else{
return b-a;
}
};
double d = doubleBinaryOperator.applyAsDouble(6.1,9.1);
double d1 = doubleBinaryOperator.applyAsDouble(61.1,9.1);
System.out.println("doubleBinaryOperator d>>"+d);
System.out.println("doubleBinaryOperator d1>>"+d1);
System.out.println("------DoubleBinaryOperator------");
}
}
输出内容:
------DoubleBinaryOperator------
doubleBinaryOperator d>>3.0
doubleBinaryOperator d1>>52.0
------DoubleBinaryOperator------
DoubleConsumer:
DoubleConsumer代表一个接受double值参数的操作,并且不返回结果。
public class MainTest {
public static void main(String[] args) {
System.out.println("------DoubleConsumer------");
DoubleConsumer doubleConsumer = System.out::println;
doubleConsumer.accept(6.5);
doubleConsumer.andThen(doubleConsumer).accept(18);
System.out.println("------DoubleConsumer------");
}
}
输出内容:
------DoubleConsumer------
6.5
18.0
18.0
------DoubleConsumer------
DoubleFunction:
代表接受一个double值参数的方法,并且返回结果.
public class MainTest {
public static void main(String[] args) {
System.out.println("------DoubleFunction------");
DoubleFunction doubleFunction = (w)->{
if (w>0){
System.out.println("正double数据");
return w*2;
}else{
System.out.println("负double数据");
return w*2;
}
};
System.out.println("DoubleFunction >>>>"+doubleFunction.apply(18));
System.out.println("DoubleFunction >>>>"+doubleFunction.apply(-18));
System.out.println("------DoubleFunction------");
}
}
输出内容:
------DoubleFunction------
正double数据
DoubleFunction >>>>36.0
负double数据
DoubleFunction >>>>-36.0
------DoubleFunction------
DoublePredicate:
代表一个拥有double值参数的boolean值方法.具体方法放在BiPredicate.
```java
public class MainTest {
public static void main(String[] args) {
System.out.println("------DoublePredicate------");
DoublePredicate doublePredicate = qw->qw>0;
DoublePredicate doublePredicate2 = qe->qe>0;
System.out.println("DoublePredicate test====="+doublePredicate.test(-11));
System.out.println("DoublePredicate negate ====="+doublePredicate.negate().test(-11));
System.out.println("DoublePredicate and ====="+doublePredicate.and(doublePredicate2).test(111));
System.out.println("DoublePredicate or ====="+doublePredicate.or(doublePredicate2).test(111));
System.out.println("------DoublePredicate------");
}
}
输出内容:
```java
------DoubleFunction------
------DoublePredicate------
DoublePredicate test=====false
DoublePredicate negate =====true
DoublePredicate and =====true
DoublePredicate or =====true
------DoublePredicate------
------DoubleFunction------