zoukankan      html  css  js  c++  java
  • lambda函数接口

    1 Consumer

          只有输入,没有输出

    // #1 函数推导
    Consumer consumer1 = (s) -> System.out.println(s);
    consumer1.accept("你好");
    // #2 第二种写法
    Consumer consumer2=System.out::println;
    consumer2.accept("洋洋");

    // #3 andThen 从前向后执行
    consumer2.andThen(consumer2).andThen(consumer1).accept("二哈"); //二哈 二哈 三娃子

    // consumer2执行,打印"二哈"
    //consumer2执行,打印"二哈"
    //consumer1执行,打印"三娃子" (consumer1的接口打印的是"三娃子")


    2 Function
    既有输入,又有输出(类似将参数经过处理并返回)
    @Test
    public void test1() {

    // #1 第一个参数,参数类型, 第二个参数:返回值类型
    Function<Integer, Integer> function = s -> s * s;
    Integer apply = function.apply(12);
    System.out.println(apply); // 144

    }

    @Test
    public void test2() {
    // #1 输入 String 返回 int 的function
    Function<String, Integer> parseInt = Integer::parseInt;
    Integer apply1 = parseInt.apply("12");
    System.out.println(apply1); // 12
    }

    @Test
    public void test3() {

    // #1 compose ()里面的先执行

    Function<Integer, Integer> function1 = s -> s * s;
    Function<Integer, Integer> function2 = s -> --s;
    Function<Integer, Integer> function3 = s -> ++s;

    // 先执行function3 (3) 再执行 function2 (2) 再执行function1 (4) 先执行的结果输出作为后执行的输入
    Integer apply = function1.compose(function2).compose(function3).apply(2);
    System.out.println(apply); //4
    }

    @Test
    public void test4() {
    Function<Integer, Integer> function1 = s -> s * s;
    Function<Integer, Integer> function2 = s -> --s;
    Function<Integer, Integer> function3 = s -> s = s + 2;

    // 先执行 function1 (9) 再执行function2(8) 再执行function3 (10)
    Integer apply = function1.andThen(function2).andThen(function3).apply(3);
    System.out.println(apply);
    }

    @Test
    public void test6() {
    // identity方法会返回一个不进行任何处理的Function,即输出与输入值相等;
    Object s = Function.identity().apply("s");
    System.out.println(s.toString()); // s
    }
    3 Predicate
       “判断”,返回boolean
    // test
    @Test
    public void test1() {

    // 传入一个参数,返回boolean
    Predicate<String> predicate = (s) -> {
    return s.endsWith("a");
    };
    //简写 Predicate<String> predicate1=s->s.endsWith("a");
    boolean b = predicate.test("asd");
    System.out.println(b);
    }


    // negate 结果取反
    @Test
    public void test2() {


    Predicate<String> predicate = s -> s.endsWith("a");
    boolean b = predicate.test("asd");
    System.out.println(b); //false

    boolean asd = predicate.negate().test("asd");
    System.out.println(asd); //true
    }

    // and 针对同一输入值,多个Predicate均返回True时返回True,否则返回False;
    @Test
    public void test3() {

    Predicate<String> predicate1 = s -> s.endsWith("a");
    Predicate<String> predicate2 = s -> s.startsWith("a");

    boolean asa = predicate1.and(predicate2).test("asa");
    System.out.println(asa); //true
    boolean ass = predicate1.and(predicate2).test("ass");
    System.out.println(ass); //false
    }
    // or 针对同一输入值,多个Predicate只要有一个返回True则返回True,否则返回False

    @Test
    public void test4() {
    Predicate<String> predicate1 = s -> s.endsWith("a");
    Predicate<String> predicate2 = s -> s.startsWith("a");

    boolean asa = predicate1.or(predicate2).test("asa");
    System.out.println(asa); //true
    boolean ass = predicate1.or(predicate2).test("ass");
    System.out.println(ass); //true
    boolean sass = predicate1.or(predicate2).test("sass");
    System.out.println(sass); //false
    }

    4
    UnaryOperator
          1元运算,接收T对象,返回T对象(继承的Function接口,只不过输入与输出的类型不能变)
    @Test
    public void unaryOperator() {
    UnaryOperator<Integer> unaryOperator = (s) -> s + 1;
    Integer apply = unaryOperator.apply(1);
    Optional.ofNullable(apply).ifPresent(System.out::println); // 2
    }

    5 BinaryOperator
       二目算符 接收两个T对象,返回T对象
    @Test
    public void binaryOperator() {
    BinaryOperator<String> binaryOperator = (m, n) -> String.valueOf(Integer.parseInt(m) + Integer.parseInt(n));
    String apply = binaryOperator.apply("1", "3");
    Optional.ofNullable(apply).ifPresent(System.out::println);
    }
    6 IntFunction
    输入参数类型 int ,输出参数类型:自定义。专门处理int类型,避免了拆箱装箱。与Function不是继承关系,而是同级的关系
    @Test
    public void intFunction(){
    IntFunction<String> intFunction =(s)->String.valueOf(s+4);
    String apply = intFunction.apply(5);
    Optional.ofNullable(apply).ifPresent(System.out::println);

    }
    
    
    .。。。。。。。。。
    这些接口在 java.util.function.function 包,还有更多
    
    


    
    




    
    





  • 相关阅读:
    在阿里云“专有网络”网络类型中配置vsftpd
    Ubuntu 16.04下开启Mysql 3306端口远程访问
    .net core 2.0 报错:error NU1102: Unable to find package ...
    .NET Core Runtime ARM32 builds now available
    .NET Core on Raspberry Pi
    .NET Core 跨平台发布(dotnet publish)
    使用vscode开发调试.net core应用程序并部署到Linux跨平台
    docker容器镜像删除
    使用阿里docker镜像加速器加速
    树莓派3b基于UbuntuMate下载中文输入法
  • 原文地址:https://www.cnblogs.com/draymond/p/11696233.html
Copyright © 2011-2022 走看看