java高级开发--内建函数式接口

Lamdba的核⼼心在于:函数式接口。⽽而函数式接口的核⼼心:只有一个抽象⽅方法。
java.util.function实际上函数式编程分为以下四种接口:

  1. 功能型函数式接⼝口:public interface Function<T, R> R apply(T t);
  2. 供给型函数式接⼝口: public interface Supplier T get();
  3. 消费型函数式接⼝口:public interface Consumer void accept(T t);
  4. 断⾔言型接⼝口:public interface Predicate boolean test(T t);
package com.bit.function;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wang
 * Date: 2019-05-19
 * Time: 22:46
 **/
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestBuildInFunction {
    /**
     y = f(x) : 给入参 x  经过处理 返回结果 y
     y = f()  : 函数提过一个结果 y
     y(void) = f(x) : 给入参 x 处理它
     y(boolean) = f(x) :  给入参x 经过处理  返回boolean类的结果 (true  false)

     */

    //功能型接⼝
    // y = f(x) : 给入参 x  经过处理 返回结果 y
    public static void function(){
//          Function<Integer,String> function = (x)->{
//              return String.valueOf(x);
//          };
        Function<Integer,String> function = String::valueOf;
        System.out.println(function.apply(10));

        //Lambda
        print((p)->{
            System.out.println("xxx");
            return String.valueOf(p);
        },20);
    }

    // 供给型接⼝
    //y = f()  : 函数提过一个结果 y
    public static void supplier(){
        Supplier<String> supplier = ()->{
            return "Hello World";
        };
        System.out.println(supplier.get());
        //T 的 get();
        // Object();-> object
        print(Object::new);
    }

    //消费型接⼝
    public  static void consumer(){
        Consumer<String> consumer = (x)->{
            System.out.println(x);
        };
        consumer.accept("Hello");
        consumer = System.out::println;
    }

    //断言型接口
    public static void predicate(){
        Predicate<String> predicate = (x) ->{
            if (x==null){
                return false;
            }else {
                return x.length()> 2;
            }
        };
        System.out.println(predicate.test("hello"));
        System.out.println(predicate.test(" "));
    }


    public static void main(String[] args) {
        //y = f()
        Supplier<String> supplier = ()->{
            Random random = new Random();
            return String.valueOf(random.nextInt(200));
        };

        //z = g(x)
        Predicate<Supplier<String>> predicate = (x) ->
        {
            String value = x.get();
            System.out.println(value);
            if (value==null){
                return false;
            }else {
                return value.length()>2;
            }
        };

        //z = g(f())
        boolean rs = predicate.test(supplier);
        System.out.println(rs);
    }

    public static void print (Function<Integer,String> function,Integer p){
        System.out.println(function.apply(p));
    }

    public static void print(Supplier<?> supplier){
        System.out.println(supplier.get());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值