深入学习java源码之Supplier.get()与Function.apply()
java函数式接口
Java中的函数式编程体现就是Lambda,所以函数式接口就是可
以适用于Lambda使用的接口。只有确保接口中有且仅有一个抽象方法,Java中的Lambda才能顺利地进行推导。
只要确保接口中有且仅有一个抽象方法即可:
@FunctionalInterface注解
与@Override 注解的作用类似,Java 8中专门为函数式接口引入了一个新的注解: @FunctionalInterface 。该注
解可用于一个接口的定义上,一旦使用该注解来定义接口,编译器将会强制检查该接口是否确实有且仅有一个抽象方法,否则将会报错。需要注
意的是,即使不使用该注解,只要满足函数式接口的定义,这仍然是一个函数式接口,使用起来都一样。
lambda表达式: (参数列表)->{代码}
有参数,有返回值的自定义函数式接口
@FunctionalInterface
interface Converter<F, T> {
T convert(F from);
}
使用:
Converter<String, Integer> converter = (from) -> Integer.valueOf(from);
Integer converted = converter.convert("123");
Lambda表达式(这里只是简单提一下)
书写方法: e -> System.out.println( e )
1. 三部分构成
参数列表
符号 ->
函数体 : 有多个语句,可以用{} 包括, 如果需要返回值且只有一个语句,可以省略 return
2. 访问控制:
可以访问类的成员变量和局部变量(非final会自动隐含转为final)
@FunctionalInterface
public interface Sumable {
int sum(int a, int b);
}
注:方法和构造函数引用在Java8中可以通过 :: 操作符调用
JDK1.8之后的某些函数式接口
Function<T, R>
apply(T t)
Function<String, String> function = a -> a + " Jack!";
System.out.println(function.apply("Hello")); // Hello Jack!
andThen(Function<? super R,? extends V> after)
Function<String, String> function = a -> a + " Jack!";
Function<String, String> function1 = a -> a + " Bob!";
String greet = function.andThen(function1).apply("Hello");
System.out.println(greet); // Hello Jack! Bob!
compose(Function<? super V,? extends T> before)
Function<String, String> function = a -> a + " Jack!";
Function<String, String> function1 = a -> a + " Bob!";
String greet = function.compose(function1).apply("Hello");
System.out.println(greet); // Hello Bob! Jack!
BiFunction<T,U,R>
接受两个参数并返回结果的函数
apply(T t, U u)
BiFunction<String, String, String> biFunction = (a, b) -> a + b;
System.out.println(biFunction.apply("Hello ", "Jack!")); // Hello Jack!
andThen(Function<? super R,? extends V> after)
Function<String, String> function = (a) -> a + "!!!";
System.out.println(biFunction.andThen(function).apply("Hello", " Jack")); // Hello Jack!!!
DoubleFunction<R>
接收一个double类型的参数并返回结果的函数
DoubleFunction<String> doubleFunction = doub -> "结果:" + doub;
System.out.println(doubleFunction.apply(1.6)); // 结果:1.6
DoubleToIntFunction
接收一个double类型的参数并返回int结果的函数
DoubleToIntFunction doubleToIntFunction = doub -> Double.valueOf(doub).intValue();
System.out.println(doubleToIntFunction.applyAsInt(1.2)); // 1
ToDoubleBiFunction<T,U>
接收两个参数并返回double结果的函数
ToDoubleBiFunction<Long, Float> toDoubleBiFunction = (lon, floa) -> lon
.doubleValue() + floa.doubleValue();
System.out.println(toDoubleBiFunction.applyAsDouble(11L, 235.5f)); // 246.5
ToDoubleFunction<T>
接收一个参数并返回double结果的函数
ToDoubleFunction<Float> toDoubleFunction = floa -> floa.doubleValue();
System.out.println(toDoubleFunction.applyAsDouble(12315f)); // 12315.0
supplier
生产数据函数式接口
目的是生产数据.
目前好像看不出来有什么用,但是好像和jdk8的Stream流有关.,举个小例子
import java.util.function.Supplier;
/**
* 使用supplier函数式接口求数组的最大值
*/
public class ArrMaxValue {
public static int getMaxValue(Supplier<Integer> sup){
return sup.get();
}
public static void main(String[] args) {
// 创建数组
int[] arr = {100,20,50,30,99,101,-50};
int maxValue = getMaxValue(()->{
int max = arr[0];
for (int i : arr) {
if(i > max){
max = i;
}
}
return max;
});
System.out.println("数组中的最大值为:" + maxValue);
}
}
Supplier<T>
无需提供输入参数,返回一个T类型的执行结果
Supplier<String> supplier = () -> "Hello Jack!";
System.out.println(supplier.get()); // Hello Jack!
BooleanSupplier
不提供输入参数,但是返回boolean结果的函数
BooleanSupplier booleanSupplier = () -> true;
System.out.println(booleanSupplier.getAsBoolean()); // true
DoubleSupplier
不提供输入参数,但是返回double结果的函数
DoubleSupplier doubleSupplier = () -> 2.7;
System.out.println(doubleSupplier.getAsDouble()); // 2.7
java源码
Modifier and Type | Method and Description |
---|---|
default <V> Function<T,V> | andThen(Function<? super R,? extends V> after) 返回一个组合函数,首先将该函数应用于其输入,然后将 |
R | apply(T t) 将此函数应用于给定的参数。 |
default <V> Function<V,R> | compose(Function<? super V,? extends T> before) 返回一个组合函数,首先将 |
static <T> Function<T,T> | identity() 返回一个总是返回其输入参数的函数。 |
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
static <T> Function<T, T> identity() {
return t -> t;
}
}
Modifier and Type | Method and Description |
---|---|
default <V> BiFunction<T,U,V> | andThen(Function<? super R,? extends V> after) 返回一个组合函数,首先将此函数应用于其输入,然后将 |
R | apply(T t, U u) 将此函数应用于给定的参数。 |
package java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(apply(t, u));
}
}
Modifier and Type | Method and Description |
---|---|
R | apply(double value) 将此函数应用于给定的参数。 |
package java.util.function;
@FunctionalInterface
public interface DoubleFunction<R> {
R apply(double value);
}
Modifier and Type | Method and Description |
---|---|
int | applyAsInt(double value) 将此函数应用于给定的参数。 |
package java.util.function;
@FunctionalInterface
public interface DoubleToIntFunction {
int applyAsInt(double value);
}
Modifier and Type | Method and Description |
---|---|
double | applyAsDouble(T t, U u) 将此函数应用于给定的参数。 |
package java.util.function;
@FunctionalInterface
public interface ToDoubleBiFunction<T, U> {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
double applyAsDouble(T t, U u);
}
Modifier and Type | Method and Description |
---|---|
double | applyAsDouble(T value) 将此函数应用于给定的参数。 |
package java.util.function;
@FunctionalInterface
public interface ToDoubleFunction<T> {
double applyAsDouble(T value);
}
Modifier and Type | Method and Description |
---|---|
T | get() 获得结果。 |
package java.util.function;
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Modifier and Type | Method and Description |
---|---|
boolean | getAsBoolean() 获得结果。 |
package java.util.function;
@FunctionalInterface
public interface BooleanSupplier {
boolean getAsBoolean();
}
Modifier and Type | Method and Description |
---|---|
double | getAsDouble() 获得结果。 |
package java.util.function;
@FunctionalInterface
public interface DoubleSupplier {
double getAsDouble();
}