深入学习java源码之Supplier.get()与Function.apply()

深入学习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 TypeMethod and Description
default <V> Function<T,V>andThen(Function<? super R,? extends V> after)

返回一个组合函数,首先将该函数应用于其输入,然后将 after函数应用于结果。

Rapply(T t)

将此函数应用于给定的参数。

default <V> Function<V,R>compose(Function<? super V,? extends T> before)

返回一个组合函数,首先将 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 TypeMethod and Description
default <V> BiFunction<T,U,V>andThen(Function<? super R,? extends V> after)

返回一个组合函数,首先将此函数应用于其输入,然后将 after函数应用于结果。

Rapply(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 TypeMethod and Description
Rapply(double value)

将此函数应用于给定的参数。

package java.util.function;

@FunctionalInterface
public interface DoubleFunction<R> {

    R apply(double value);
}
Modifier and TypeMethod and Description
intapplyAsInt(double value)

将此函数应用于给定的参数。

package java.util.function;

@FunctionalInterface
public interface DoubleToIntFunction {

    int applyAsInt(double value);
}
Modifier and TypeMethod and Description
doubleapplyAsDouble(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 TypeMethod and Description
doubleapplyAsDouble(T value)

将此函数应用于给定的参数。

package java.util.function;

@FunctionalInterface
public interface ToDoubleFunction<T> {

    double applyAsDouble(T value);
}
Modifier and TypeMethod and Description
Tget()

获得结果。

package java.util.function;

@FunctionalInterface
public interface Supplier<T> {

    T get();
}
Modifier and TypeMethod and Description
booleangetAsBoolean()

获得结果。

package java.util.function;

@FunctionalInterface
public interface BooleanSupplier {

    boolean getAsBoolean();
}

 

Modifier and TypeMethod and Description
doublegetAsDouble()

获得结果。

package java.util.function;

@FunctionalInterface
public interface DoubleSupplier {

    double getAsDouble();
}

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wespten

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值