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

    函数式接口

    在java8中,新增了很多函数式接口。在接口上标记了注解@FunctionalInterface的都是函数式接口
    比如 Function、Supplier、Consumer、Predicate、BiFunction、BiConsumer、BiPredicate。

    Function接口

    Function源码如下:

    @FunctionalInterface
    public interface Function<T, R> {
        R apply(T t);
    }
    

    Function接口的apply()方法,有一个入参,还有返回值。

    • 实现接口,重写方法

    我们可以实现Function接口,重写apply()方法。
    示例如下:

    public class MyFunction implements Function<T, String> {
        @Override
        public String apply(T t) {
            return "test";
        }
    }
    
    • 使用匿名类简化:

    重写Function接口比较麻烦,还需要新建一个类。可以直接用匿名类。

    示例如下:

        public static void functionTest() {
            Function<T, String> function = new Function<T, String>() {
                @Override
                public String apply(T t) {
                    return "test";
                }
            };
    
        }
    
    • 使用lambda表达式简化:

    lambda表达式,其实就是匿名函数。
    lambda表达式的格式为:

    (参数) -> 返回值;
    
    (参数) ->{ 代码块; }
    

    比如 (x, y) -> x + y; ,表示方法的参数是x和y,方法的返回值是 x+y的和。

    详情见: https://www.cnblogs.com/expiator/p/12297003.html

    使用使用lambda表达式简化上面的Function例子:

        public static void functionTest() {
            Function<T, String> function = t -> "test";
        }
    

    如果函数内有其他内容,也可以如下:

    
            Function<T, String> function2 = t -> {
                 //函数的内容
                return "test";
            };
    

    函数式接口的运用

    通过lambda表达式,我们可以很方便地使用函数式接口,最常见的就是将函数式接口作为方法的参数。

    在排序时,经常会使用Comparator接口,comparing()方法就将Function类型作为方法参数。
    源码如下:

    comparing(Function<? super T, ? extends U> keyExtractor)
    

    示例如下:

    /**
     *   根据数字除以10的余数,进行排序
     */
    public static void comparingTest() {
            List<Integer> list = Arrays.asList(0, 1, 23, 45, 67, 81);
            List<Integer> resultList = list.stream()
                                            .filter( n -> n!=0)
                                            .sorted(Comparator.comparing(num -> num % 10))
                                            .collect(Collectors.toList());
            System.out.println(resultList);
        }
    
    

    可以看到,此处的 num -> num%10 就是一个Function类型作为方法的参数。

    Supplier接口

    @FunctionalInterface
    public interface Supplier<T> {
        T get();
    }
    

    Supplier接口的get()方法,没有参数,有返回值。

    示例如下:

    Supplier<String> supplier = () -> {
        //函数的内容    
        return "test";
    };
    

    Consumer接口

    @FunctionalInterface
    public interface Consumer<T> {
        void accept(T t);
    }
    

    Consumer接口的accept()方法,一个参数,没有返回值。
    示例如下:

    Consumer<String> consumer = (var) -> {
        //函数的内容
    };
    

    Predicate接口

    @FunctionalInterface
    public interface Predicate<T> {
        boolean test(T t);
    }
    

    Predicate接口的test()方法,一个参数,返回一个布尔值。
    示例如下:

    Predicate<String> predicate = (var) -> {    
        //函数的内容
        return true;
    };
    

    BiFunction接口

    @FunctionalInterface
    public interface BiFunction<T, U, R> {
        R apply(T t, U u);
    }
    

    BiFunction接口的apply()方法,两个入参,有返回值。

    示例如下:

    BiFunction<String, String, String> biFunction = (a, b) -> {
       //函数的内容
        return a + b; 
    };
    

    BiConsumer接口

    @FunctionalInterface
    public interface BiConsumer<T, U> {
        void accept(T t, U u);
    

    BiConsumer接口有两个参数,没有返回值。

    BiConsumer<String,String> biConsumer = (String a, String b) -> {
       //函数的内容
    };
    
  • 相关阅读:
    kuangbin专题 专题二 搜索进阶 Escape HDU
    kuangbin专题 专题二 搜索进阶 哈密顿绕行世界问题 HDU
    kuangbin专题 专题一 简单搜索 Find a way HDU
    kuangbin专题 专题一 简单搜索 非常可乐 HDU
    kuangbin专题 专题一 简单搜索 Oil Deposits HDU
    kuangbin专题 专题一 简单搜索 迷宫问题 POJ
    Java FileWriter类
    如何将多行中的文本连接成SQL服务器中的单个文本字符串String?
    fread (File input/output) – C 中文开发手册
    ASP.NET Table 控件
  • 原文地址:https://www.cnblogs.com/expiator/p/14841355.html
Copyright © 2011-2022 走看看