zoukankan      html  css  js  c++  java
  • (转)JDK1.8新特性Lambda表达式

    https://www.cnblogs.com/franson-2016/p/5593080.html

    Predicate 
    predicate接收一个变量,并返回一个boolean值,predicate接口是一个简单的比进行较运算操作的接口,但一般不使用。例如:

    Predicate<String> p = (str) -> str.equls("321aiyi.com");
    sysout(p.test("321aiyi.com"));            //true
    sysout(p.test("321aiyi.com").negate())    //false

    Function 
    这个接口其实就是一个单纯的最简单的实现了@FunctionalInterface的接口类,一般对一个方法进行引用时,可以直接使用该接口。

    Function<String, Integer> fun = String::valueOf;
    String str = fun.apply(1);

    Supplier 
    这个就更简单了,他就相当于类的Factory,用它获取任意一个带有空参构造的类。

    Supplier<StringBuffer> s = StringBuffer::new;
    StringBuffer sb = s.get();

    Consumer 
    给定一个参数,并对其进行操作

    Consumer<String> c = (str) -> System.out.pringln(str + "~~~");
    c.accept("woca");        //"woca~~~"

    另外,还有好多之前的接口也做了对lambda的支持,比如之前所说的Comparator接口等等,这里就不一一诉说了,喜欢钻研的朋友们可以拔源码或者阅读相关文档慢慢看。

  • 相关阅读:
    input 特殊字符限制
    angular $http服务
    angular $resouse服务
    ng-model-options 时延
    Pytorch之数据处理
    python 小顶堆
    刷题套路总结
    数组分成和尽可能相等的子数组
    python3.7 sorted 自定义排序
    Leetcode 二维数组周游 54
  • 原文地址:https://www.cnblogs.com/s648667069/p/8863711.html
Copyright © 2011-2022 走看看