文章目录
四大函数式接口(重点)
新特性:lambda表达式、链式编程、函数式接口、Stream流式计算
函数式接口:只有一个方法的接口
在新版本的框架中大量使用,可简化编程模型
@FunctionalInterface
public interface Runnable {
public abstract void run();
}
- 四大原生函数式接口
- 代码测试:
函数式接口
Function
@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);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this 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 input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* 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
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
*
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
- 函数式接口:有一个输入参数,有一个输出参数
- 只要是函数式接口,都可以用lambda来表示
public class FunctionTest {
public static void main(String[] args) {
// 接口利用匿名内部类创建
Function function = new Function<String,String>() {
@Override
public String apply(String s) {
return s;
}
};
// 利用函数式接口替代匿名内部类
Function<String,String> function1 = (str)->{return str;};
System.out.println(function.apply("abc"));
}
}
断定型接口:Predicate
- 是断定型接口:有一个输入参数,返回值只能是布尔值
public class PredicateTest {
public static void main(String[] args) {
// 匿名内部类
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
return false;
}
};
// lambda表达式简化
Predicate predicate1 = (str)->{
return str.equals("123");
};
}
}
消费型接口:Consumer
- 只有输入,没有返回值
public class ConsumerTest {
public static void main(String[] args) {
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println("消费型接口");
}
};
Consumer<String> consumer1 = (str) -> {
System.out.println("消费型接口lambda表达式");
};
}
}
供给型接口:
- 没有输入,只有返回值
public class SupplierTest {
public static void main(String[] args) {
Supplier supplier = new Supplier<Integer>() {
@Override
public Integer get() {
return null;
}
};
Supplier supplier1 = ()->{return 123;};
}
}