函数式编程

目录

一:自定义函数

Type1 

Type2

Type3

Type4

Type5

Type6

Type7

 二:四大函数式接口

 Function 函数型接口,>

Predicate 断定型接口

Consumer 消费型接口

Supplier 供给型接口

其他函数式接口(扩展)

函数型接口

判断型接口

消费型接口

供给型接口


一:自定义函数

抽象出的函数

public static void main(String[] args) {
    Type1 type1 = a -> (a & 1) == 0;
    Type2 type2 = (a, b, c) -> a + b + c;
    Type3 type3 = () -> new Student();
    Type4 type4 = () -> new ArrayList<Student>();

    // 利用泛型
    Type5<Student> type5 = () -> new Student();
    Type6<Student> type6 = student -> {student.getAge();
        return student;
    };

    // 两个输入参数,返回一个参数
    Type7<String, Student> type7 = student -> {
        return student.getName();
    };
}

 函数式接口

Type1

/**
 * 函数式接口,我们只关注参数和返回值类型
 */
@FunctionalInterface
interface Type1{
    boolean op(int a);
}

               

 Type2

@FunctionalInterface
interface Type2{
    int op(int a, int b, int c);
}

 Type3

@FunctionalInterface
interface Type3{
    Student op();
}

 Type4

@FunctionalInterface
interface Type4{
    List<Student> op();
}

 Type5

/**
 * 利用泛型进行扩展
 * @param <T>
 */
@FunctionalInterface
interface Type5<T> {
    T op();
}

 Type6

@FunctionalInterface
interface Type6<T> {
    T op(Student student);
}

 Type7

// Type6把入参也进行泛型,输入I类型返回T类型
@FunctionalInterface
interface Type7<T, I> {
    T op(I input);
}

 二:四大函数式接口

 Function<T, R> 函数型接口

表示接收一个参数并返回结果的函数。该函数式接口方法为R apply(T t)

<T> 为入参泛型

<R> 为返回结果泛型

示例代码一:

Function<Integer, String> function = new Function<Integer, String>() {
        @Override
        public String apply(Integer integer) {
            return integer.toString() + "程序员节";
        }
    };
System.out.println("demo1 --> " + function.apply(1024));

示例代码二:函数式接口做为方法的入参

static void functionDemo2(Integer integer, Function<Integer, String> function) {
    System.out.println("demo2 --> " + function.apply(integer));
}

public static void main(String[] args) {
	functionDemo4(1024, integer -> integer.toString() + "程序员节");
}

Predicate<T> 断定型接口

表示接收一个参数,判断该参数是否满足某约束,并返回boolean值。该函数式接口方法为boolean test(T t)

<T> 为入参泛型

返回boolean型值

示例代码一:

static void predicateDemo1() {
    Predicate<Integer> predicate = integer -> integer == 1024;
    System.out.println("predicateDemo1 --> " + predicate.test(1024));
}

示例代码二:

static void predicateDemo2(Integer integer, Predicate<Integer> predicate) {
    System.out.println("predicateDemo1 --> " + predicate.test(integer));
}

public static void main(String[] args) {
	predicateDemo2(1024, integer -> integer == 1024);
}

Consumer<T> 消费型接口

接收一个参数但不返回结果,表示为消费者。该函数式接口方法为void accept(T t)

<T> 为入参泛型

示例代码一:

static void consumerDemo1() {
    Consumer<Integer> consumer = integer -> System.out.println("consumerDemo1 --> " + integer);
    consumer.accept(1024);
}

示例代码二: 

static void consumerDemo2(Integer integer, Consumer<Integer> consumer) {
    consumer.accept(integer);
}

public static void main(String[] args) {
    consumerDemo2(1024, integer -> System.out.println("consumerDemo2 --> " + integer));
}

 示例代码三:

public static void consume(String s, Consumer<String> con1, Consumer<String> con2) {
        con1.andThen(con2).accept(s);
        // 完全等价于
        // con1.accept(s);
        // con2.accept(s);
    }
public static void printer(Consumer<String> con1, Consumer<String> con2, String[] arr){
        for(String s : arr){
            con1.andThen(con2).accept(s);
        }
    }

    public static void main(String[] args) {
        String[] arr = {"Alice,12", "Chino,10", "Cocoa,9"};
        printer((s)->{												
            System.out.println("姓名:" + s.split(",")[0] + ";");
        }, (s)->{
            System.out.println("年龄:" + s.split(",")[1] + ".\n");
        }, arr);
    }

输出:
姓名:Alice;
年龄:12.

姓名:Chino;
年龄:10.

姓名:Cocoa;
年龄:9.

Supplier<T> 供给型接口

Consumer相反,Supplier只有返回结果,表示为提供者。该函数式接口方法为T get()

<T> 为返回结果泛型

代码示例一:

static void supplierDemo1() {
    Supplier<Integer> supplier = () -> 1024;
    System.out.println("supplierDemo1 --> " + supplier.get());
}

代码示例二:
 

static void supplierDemo2(Supplier<Integer> supplier) {
    System.out.println("supplierDemo2 --> " + supplier.get());
}

public static void main(String[] args) {
    supplierDemo2(() -> 1024);
}

其他函数式接口(扩展)

函数型接口
函数式接口参数类型返回类型方法用途
BiFunction<T,U,R>T,URR apply(T t, U u)接收两个输入参数并产生结果
ToIntBiFunction<T,U>T,Uintint applyAsInt(T t,U u)接收两个输入参数并返回int类型结果
ToLongBiFunction<T,U>T,Ulonglong applyAsLong(T t, U u)接收两个输入参数并返回long类型结果
ToDoubleBiFunction<T,U>T,Udoubledouble(T t, U u)接收两个输入参数并产生double类型结果
ToIntFunction<T>Tintint applyAsInt(T value)接收一个参数并返回int类型结果
ToDoubleFunction<T>Tdoubledouble applyAsDouble(T value)接收一个参数并返回double类型结果
IntFunction<R>intRR apply(int value)接收一个int类型参数,并返回结果
LongFunction<R>longRR apply(long value)接收一个long类型参数,并返回结果
DoubleFunction<R>doubleRR apply(double value)接收一个double类型参数,并返回结果
IntToDoubleFunctionintdoubledouble applyAsDouble(int value)接收一个int类型参数,返回double类型结果
LongToDoubleFunctionlongdoubledouble applyAsDouble(long value)接收一个long类型参数,返回double类型结果
LongToIntFunctionlongintint applyAsInt(long value)接收一个long类型参数,返回int类型结果
IntToLongFunctionintlonglong applyAsLong(int value)接收一个int类型参数,返回long类型结果
DoubleToIntFunctiondoubleintint applyAsInt(double value)接收一个double类型参数,返回int类型结果
DoubleToLongFunctiondoublelonglong applyAsLong(double value)接收一个double类型参数,返回long类型结果
BinaryOperator<T>T,TTT apply(T t1, T t2)BiFunction<T,T,T>的一个子接口,表示对同一类型的两个参数进行操作,产生同一类型的结果
UnaryOperator<T>TTT apply(T t)Function<T,T>的一个子接口,表示对单个参数操作,产生同一类型的结果
LongBinaryOperatorlong,longlonglong applyAsLong(long left, long right)对两个long值计算并产生long结果
DoubleBinaryOperatordouble,doubledoubledouble applyAsDouble(double left, double right)对两个double值计算并产生double结果
IntUnaryOperatorintintint applyAsInt(int operand)对单个int值操作,产生一个int结果
LongUnaryOperatorlonglonglong applyAsLong(long operand)对单个long值操作,产生一个long结果
DoubleUnaryOperatordoubledoubledouble applyAsDouble(double operand)对单个double值操作,返回一个double结果
IntBinaryOperatorint,intintint applyAsInt(int left, int right)对两个int值计算并产生int结果
判断型接口
函数式接口参数类型返回类型方法用途
IntUnaryOperatorintintint applyAsInt(int operand)对单个int值操作,产生一个int结果
LongUnaryOperatorlonglonglong applyAsLong(long operand)对单个long值操作,产生一个long结果
DoubleUnaryOperatordoubledoubledouble applyAsDouble(double operand)对单个double值操作,返回一个double结果
IntBinaryOperatorint,intintint applyAsInt(int left, int right)对两个int值计算并产生int结果
消费型接口
函数式接口参数类型返回类型方法用途
BiConsumer<T,U>T,Uvoidvoid accept(T t, U u)接收两个输入参数,不返回结果
ObjIntConsumer<T>T,intvoidvoid accept(T t, int value)接收对象值和int类型参数,不返回结果
ObjLongConsumer<T>T,longvoidvoid accept(T t, long value)接收对象值和long类型参数,不返回结果
ObjDoubleConsumer<T>T,doublevoidvoid accept(T t, double value)接收对象值和double类型参数,不返回结果
LongConsumerlongvoidvoid accept(long value)消费long类型值,不返回结果
DoubleConsumerdoublevoidvoid accept(double value)消费double类型值,不返回结果
IntConsumerintvoidvoid accept(int value)消费int类型值,不返回结果
供给型接口
函数式接口参数类型返回类型方法用途
BooleanSupplierbooleanboolean getAsBoolean()表示供给boolean类型的结果
IntSupplierintint getAsInt()表示供给int类型的结果
LongSupplierlonglong getAsLong()表示供给long类型的结果
LongSupplierlonglong getAsLong()表示供给long类型的结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值