目录
static?default? lamda表达式?
package com.wanke.microgrid.mgcc;
@FunctionalInterface
public interface NullAnnotation {
//抽象方法
public void getName(String a);
boolean equals(Object obj);
//default方法
default void getAge(){
System.out.println("age");
}
default void getAge1(){
}
//静态方法
static void static1(){
System.out.println("static1");
}
static void static2(){
}
}
package com.wanke.microgrid.mgcc;
public class Test{
public static void main(String[] args) {
String a = "aaaa";
//Lambda表达式
NullAnnotation annotation = (f) -> System.out.println(f);
annotation.getName(a);
//defalut方法属于对象的默认方法
annotation.getAge();
//static属于类的方法
NullAnnotation.static1();
}
}
运行结果:
aaaa
age
static1
@FunctionalInterface标记接口为函数式接口,函数式接口有且只能有一个抽象方法,或许大家会问你不是写了两个抽象方法吗?java.lang.Object根对象有equals方法吧,所以实现该类的时候是不是不必须要实现这个接口(所有的类都是实现java.lang.Object的)。Lambda表达式是基于函数式接口的
常见的函数式接口
package com.wanke.microgrid.mgcc;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.*;
public class Test {
//Consumer消费型接口,有参无返回
static void testConsumer(Integer x, Consumer<Integer> consumer) {
consumer.accept(x);
}
//suplier供给型接口,无参有返回值
static void testSupplier(Supplier<Integer> supplier) {
System.out.println(supplier.get());
}
static Integer getInteger() {
return 2;
}
//函数式接口,有参有返回值
static void testFunction(Integer num, Function<Integer, Integer> function) {
System.out.println(function.apply(num));
}
//断言型接口,有参有返回值,返回值是boolean类型
static void testPredicate(Integer num, Predicate<Integer> predicate) {
System.out.println(predicate.test(num));
}
//介绍部分拓展接口
//BinaryOperator (R apply(T t, U u) ->两个输入,一个输出,而且类型一样)
public static void binaryOperatorCompute(Integer para1, Integer para2, BinaryOperator<Integer> binaryOperator) {
//使用自定义BinaryOperator
System.out.println(binaryOperator.apply(para1, para2));
Comparator<Integer> cpt2 = (x, y) -> Integer.compare(x, y);
//静态方法传入比较器生成BinaryOperator
BinaryOperator<Integer> min = BinaryOperator.minBy(cpt2);
System.out.println(min.apply(para1, para2));
}
//toIntFunction (int applyAsInt(T t, U u) ->两个输入,一个int输出)
public static void testToIntFunction(Integer para1, Integer para2, ToIntBiFunction<Integer, Integer> toIntBiFunction) {
System.out.println(toIntBiFunction.applyAsInt(para1, para2));
}
//BiFunction (R apply(T t, U u) ->两个输入,一个输出)
public static void testBi(Integer para1,Integer para2,BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function) {
BiFunction<Integer, Integer, Integer> newBi = biFunction.andThen(function);
System.out.println(newBi.apply(para1,para2));
}
//如何看出输入输出,以BiFunction为例
//源码如下:
//package java.util.function;
//
//import java.util.Objects;
//
// /**
// * Represents a function that accepts two arguments and produces a result.
// * This is the two-arity specialization of {@link Function}.
// *
// * <p>This is a <a href="package-summary.html">functional interface</a>
// * whose functional method is {@link #apply(Object, Object)}.
// *
// * @param <T> the type of the first argument to the function
// * @param <U> the type of the second argument to the function
// * @param <R> the type of the result of the function
// *
// * @see Function
// * @since 1.8
// */
// @FunctionalInterface
// public interface BiFunction<T, U, R> {
//
// /**
// * Applies this function to the given arguments.
// *
// * @param t the first function argument
// * @param u the second function argument
// * @return the function result
// */
// R apply(T t, U u);
//
// /**
// * Returns a composed function that first applies this function to
// * its input, and then applies the {@code after} function to the result.
// * If evaluation of either function throws an exception, it is relayed to
// * the caller of the composed function.
// *
// * @param <V> the type of output of the {@code after} function, and of the
// * composed function
// * @param after the function to apply after this function is applied
// * @return a composed function that first applies this function and then
// * applies the {@code after} function
// * @throws NullPointerException if after is null
// */
// default <V> java.util.function.BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
// Objects.requireNonNull(after);
// return (T t, U u) -> after.apply(apply(t, u));
// }
// }
//函数式接口类型的唯一抽象方法 R apply(T t, U u);显然是量输入一输出,而且是泛型
public static void main(String[] args) {
testConsumer(1, x -> System.out.println(x));
testSupplier(() -> getInteger());
testFunction(100, x -> x + 100);
testPredicate(100, x -> x.equals(