美文网首页
JAVA8函数式接口学习

JAVA8函数式接口学习

作者: Snow_DZG | 来源:发表于2016-08-08 18:06 被阅读383次

JAVA8函数式接口

函数式接口是java8的一种新特性,函数式接口定义了且只定义了一个抽象方法!该接口非常有用,抽象方法的签名就是可以描述lambda表达式的签名。例子如下:

//自定义了接口
@FunctionalInterface
public interface BufferReaderProcessor {
    public String process(BufferedReader b) throws IOException;
}

public static String execute(BufferReaderProcessor p) throws IOException{
    try(BufferedReader br = new BufferedReader(new FileReader("SVN.txt"))){
        return p.process(br);
    }
}

String oneLine = execute((BufferedReader br) -> br.readLine());//该lambda返回的是String
String TwoLine = execute((BufferedReader br) -> br.readLine() + br.readLine());
System.out.println(oneLine + "- -" + TwoLine);

该接口表达式功能即为 将BufferedReader转换为String

接下来是 3个常用的函数式接口:

1.Predicate接口

/**
 * Predicate接口定义了一个test方法,接受泛型T对象,返回的是一个boolean。
 */
public static <T> List<T> filter(List<T> list,Predicate<T> p){
    List<T> result = new ArrayList<T>();
    for(T t:list){
        if(p.test(t)){
            result.add(t);
        }
    }
    return result;
}

2.Consumer接口

/**
 * Consumer接口定义了一个accept的方法,接受泛型T对象,无返回void
 */
public static <T> void forEach(List<T> list,Consumer<T> c){
    for(T i:list){
        c.accept(i);
    }
}

3.Function接口

/**
 * Function接口定义了一个apply的方法,接受泛型为T的对象,返回泛型为R的对象
 */
public static <T,R> List<R> map(List<T> list,Function<T,R> f){
    List<R> listR = new ArrayList<>();
    for(T t:list){
        listR.add(f.apply(t));
    }
    return listR;
}

测试:

public static void main(String[] args) throws IOException {
    List<String> Strings = filter(Arrays.asList("1","2","3"),(String s) -> !s.isEmpty());//Predicate示例
    forEach(Strings,(String i) -> System.out.println(i));//Consumer示例
    List<Integer> ints = map(Arrays.asList("Lambda","in","action"),(String s) -> s.length());//Function示例 从String类型变为Integer类型
    forEach(ints,(Integer i) -> System.out.println("长度:"+i));//Consumer示例
}

附录1:Lambda及函数式接口的例子:

使用案例 lambda例子 对应函数式接口
布尔表达式 (List<String> list) -> list.isEmpty() Predicate<List<String>>
创建对象 () -> new Fan() Supplier<Fan>
消费一个对象void (Fan f) -> sys...out(f.toString) Consumer<Fan>
从一个对象中选择提取 (String s) -> s.length() Function<String,Integer> 或 ToIntFunction<String>
合并2个值 (int a , int b) -> a+b IntBinaryOperator
比较2个对象 (Fan a , Fan b) -> a.getXX().compareTo(b.getXX()) Comparator<Fan> 或 BiFunction<Fan,Fan,Integer> 或 ToIntBiFunction<Fan.Fan>

附录2:Java8中常用的函数式接口:

函数式接口 函数描述符 原始类型特化
Predicate<T> T -> boolean IntPredicate<T>, LongPredicate<T>, DoublePredicate<T>
Consumer<T> T -> void IntConsumer, LongConsumer, DoubleConsumer
Function<T,R> T -> R IntFunction<R>, IntToDoubleFunction, IntToLongFunction, LongFunction<R>, LongToDoubleFunction, LongToIntFunction, DoubleFunction<R>, ToIntFunction<T>, ToDoubleFunction<T>, ToLongFunction<T>
Supplier<T> () -> T BooleanSupplier, BooleanSupplier, BooleanSupplier, BooleanSupplier
UnaryOperator<T> T -> T IntUnaryOperator, DoubleUnaryOperator, LongUnaryOperator
BinaryOperator<T> (T,T) -> T IntBinaryOperator, DoubleBinaryOperator, LongBinaryOperator
BiPredicate<L,R> (L,R) -> boolean
BiConsumer<T,U> (T,U) -> void ObjIntConsumer<T>, ObjLongConsumer<T>, ObjDoubleConsumer<T>
BiFunction<T,U,R> (T,U) -> R ToIntBiFunction<T,U>, ToLongBiFunction<T,U>, ToDoubleBiFunction<T,U>

相关文章

  • JAVA8函数式接口学习

    JAVA8函数式接口 函数式接口是java8的一种新特性,函数式接口定义了且只定义了一个抽象方法!该接口非常有用,...

  • 函数式接口

    函数式接口 声明:java8新特性系列为个人学习笔记,参考地址点击这里,侵删!! 函数式接口(Functional...

  • java8系列-02 函数式接口(Function、Consum

    上一章已经说了函数式接口的基本概念(java8系列-01 Lambdas 表达式)。函数式接口(Functiona...

  • JAVA8新特性之函数式接口使用与学习

    函数式接口 函数式接口是Java8引用的一个新特性,是一种特殊的接口 SAM类型的接口(Single Abstra...

  • Java8值函数式接口

    Java8值函数式接口 如果一个接口中,只声明了一个抽象方法,则此接口就称为函数式接口 在接口上声明@Functi...

  • java8 lambda

    java8 lambda 函数式接口 谓词 Predicate boolean test(T t); T -...

  • Lambda表达式

    Lambda表达式是Java8的一个新特性,是函数式接口的一种体现。所谓函数式接口(functional inte...

  • 函数式接口(二)

    前面的文章中说明了Java8中最基本的4种函数式接口,我们今天先介绍其它Cosumer函数式接口。 BiConsu...

  • Java 8 新特性 - 函数式接口 Functional In

    Java8的其中一个新特性,函数式接口。 什么是函数式接口?有且仅有一个抽象方法的接口(不包括默认方法、静态方法以...

  • Java8函数式编程之四: 常见的函数式接口及实例

    上一篇博客中Java8函数式编程之三:函数式接口 - 简书留下的问题是关于Consumer接口的,本篇博客就来介绍...

网友评论

      本文标题:JAVA8函数式接口学习

      本文链接:https://www.haomeiwen.com/subject/fuyrsttx.html