任何接口,如果只包含唯一 一个抽象方法,那么它就是一个函数式接口。比如多线程中的Runnable接口,只包含一个run()抽象方法,所以Runnable接口是一个函数式接口。
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
注1:@FunctionalInterface 注解是 Java 8为函数式接口引入的新注解。@FunctionalInterface主要用于编译期错误检查,如果标注了该注解的接口不符合函数式接口的定义,那么就会编译不通过。


注2:函数式接口有且仅有一个抽象方法,但可以有其它非抽象方法

注3:@FunctionalInterface注解不是必须的,如果一个接口符合 函数式接口 定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。
对于函数式接口,我们可以使用 java 8提供的新特性 Lambda表达式来创建该接口的对象。
//匿名内部类创建线程
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("$*****$");
}
}).start();
||
||
\/
//lambda表达式创建线程
new Thread(() -> System.out.println("lambda表达式")).start();
源码:
/**
* Represents a function that accepts one argument and produces a result.
*
* This is a functional interface
* whose functional method is {@link #apply(Object)}.
*
* @param the type of the input to the function
* @param the type of the result of the function
*
* @since 1.8
*/
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
}
使用:接收一个传入参数,并返回一个结果的操作。
import java.util.function.Function;
public class Function_01 {
public static void main(String[] args) {
// 接受一个参数并产生结果的函数
Function<String, String> function = (str)->{ return str; };
System.out.println(function.apply("Function 的函数式接口"));
}
}
源码:
/**
* Represents a predicate (boolean-valued function) of one argument.
*
* This is a functional interface
* whose functional method is {@link #test(Object)}.
*
* @param the type of the input to the predicate
*
* @since 1.8
*/
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument. // 传入一个参数,返回一个 boolean 值
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
}
使用:传入一个参数,返回一个boolean值。
public class Function_01 {
public static void main(String[] args) {
// 传入一个参数,返回一个 boolean 值
Predicate<Integer> predicate = (num)->{ return num == 100; };
System.out.println(predicate.test(99));
Predicate<String> predicate = (str)->{ return str.isEmpty(); };
System.out.println(predicate.test("哈哈"));
}
}
源码:
/**
* Represents an operation that accepts a single input argument and returns no
* result. Unlike most other functional interfaces, {@code Consumer} is expected
* to operate via side-effects.
*
* This is a functional interface
* whose functional method is {@link #accept(Object)}.
*
* @param the type of the input to the operation
*
* @since 1.8
*/
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument. //对给定参数进行操作
*
* @param t the input argument
*/
void accept(T t);
}
使用:
public class Function_01 {
public static void main(String[] args) {
// 接受一个参数,并且不返回结果的函数
Consumer<String> consumer = (str)->{ System.out.println("Consumer 消费者函数式接口" + " 参数:" + str); };
consumer.accept("00000");
}
}
源码:
/**
* Represents a supplier of results.
*
* There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
*
This is a functional interface
* whose functional method is {@link #get()}.
*
* @param the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
使用:
public class Function_01 {
public static void main(String[] args) {
// 返回一个指定类型的结果
Supplier<Integer> supplier = () -> {return 10 >> 1;};
System.out.println(supplier.get());
}
}