美文网首页
Java8的新特性--函数式接口

Java8的新特性--函数式接口

作者: 是茜茜呀 | 来源:发表于2021-03-15 08:03 被阅读0次

函数式接口

Lambda表达式需要接口的支持,并且接口的抽象方法还只能有一个,要么没法区分实现的是哪一个抽象方法了。因此Lambda表达式需要函数式接口的支持

什么是函数式接口

接口中只有一个抽象方法的接口称为函数式接口。

函数式接口可以使用一个注解@FunctionalInterface修饰,此注解可以检查是否是函数式接口

函数式接口的使用

假设我们现在有一个需求:对一个数进行运算,什么运算都可以。如果我们想用Lambda表达式来实现的话,我们就需要一个接口来支持,下面我们先写一个MyFunction接口

@FunctionalInterface
public interface MyFunction {
    public Integer getValue(Integer num);
}

然后我们需要一个运算的方法

    //运算方法
    public Integer operation(Integer num, MyFunction fun){
        return fun.getValue(num);
    }

测试类

    @Test
    public void test06(){
        //平方运算
        Integer num = operation(10,(x) -> x*x);
        System.out.println(num);
        //加和运算
        Integer num2 = operation(100,(y) -> y+200);
        System.out.println(num2);

    }

执行结果

100
300

不管是什么运算,我们只需要关注Lambda表达式的方法体如何实现运算就可以了。

通过以上例子发现,如果我们想用Lambda表达式来实现的话,我们就需要写一个接口来支持,是不是也挺麻烦的,其实,一般我们常用的接口,Java8里基本都已经给我们提供了,下面来看下Java8内置的核心接口。

Java8内置的四大核心函数式接口

一、Consumer<T>:消费型接口(void accept(T t))

我们先看一下Java8中给我定义好的Consumer接口

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

然后我们自己写个简单的例子看一下,写个开开森森去消费的方法

    @Test
    public void test01(){
        happy("一消费就很开森啦啦啦",str -> System.out.println(str));
    }
    public void happy(String str,Consumer<String> consumer){
        consumer.accept(str);
    }

运行结果

一消费就很开森啦啦啦

以上就是消费型接口,消费即是有去无回,有参数无返回值类型的接口。

二、Supplier<T>:供给型接口(T get())

我们先看一下Java8中给我定义好的Supplier接口

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

我们再来写个例子,写个方法产生指定个数的整数并放入集合中

    @Test
    public void test02(){
        List<Integer> numList = getValue(10,() -> (int)(Math.random()*100));
        for (Integer num :
                numList) {
            System.out.println(num);
        }
    }

    //产生指定个数的整数并放入集合中
    public List<Integer> getValue(int num,Supplier<Integer> supplier){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }

执行结果

84
22
60
29
52
34
9
95
34
6

以上就是供给型接口,只有产出,没有输入,即只有返回值没有入参,给你产生一些对象,并返回给你。

三、Function : 函数型接口(R apply(T t))

Java8中函数式接口的定义

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
    
    ····后面代码省略了····
    }

下面写个处理字符串方法的例子

    @Test
    public void test03(){
        String returnStr = strHandler("\t\t\t我是一串有空格\t的字符串\t\t",(str) -> str.trim());
        System.out.println(returnStr);
    }

    public String strHandler(String str,Function<String,String> function){
        return function.apply(str);
    }

运行结果:trim()方法只是去除首尾的字符串

我是一串有空格 的字符串

以上就是一个函数型接口,一个参数,一个返回值,当然参数和返回值的类型可以相同

四、Predicate<T>:断言型接口(boolean test(T t))

Java8中断言型接口的定义如下

@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
    ····后面到吗省略····
    }

下面我们写一个断言的例子,写个方法将满足条件的字符串放入集合中

    @Test
    public void test04(){
        List<String> list = Arrays.asList("aa","bbb","ccccc","dddddd");
        //长度大于3的字符串放入集合中
        List<String> reList = filterStr(list,(s) -> s.length() >3);
        for (String str: reList) {
            System.out.println(str);
        }
    }

    public List<String> filterStr(List<String> strList,Predicate<String> pre){
        List<String> list = new ArrayList<>();
        for (String str: strList) {
            if(pre.test(str)){
                list.add(str);
            }
        }
        return list;
    }

运行结果

ccccc
dddddd

以上是断言型接口,输入一个参数,输出一个boolean类型的返回值

五、其他类型的一些函数式接口

除了上面四种类型的核心接口还有一些其他的不常用的接口,我们可以了解一下

  1. BiFunction<T, U, R>
  • 类型:参数类型T,U返回类型R
  • 用途:Function的子接口,对类型为T和U的参数应用操作,返回R类型的结果。包含有方法R apply(T t,U u)
  1. UnaryOperator<T>(Function子接口)
  • 类型:参数类型T,返回类型T
  • 用途:Function的子接口,对类型为T的对象进行一元运算,并返回T类型的结果。包含有方法T apply(T t)
  1. BinaryOperator<T>(BiFunction子接口)
  • 类型:参数类型T,T,返回类型T
  • 用途:BiFunction的子接口,对类型为T的对象进行二元运算,并返回T类型的结果。包含有方法T apply(T t1,T t2)
  1. BiConsumcr(T, U)
  • 类型:参数类型T, U,返回类型void
  • 用途:Consumcr的子接口,对类型为T,U的参数应用操作。包含有方法void accept(T t,U u)
  1. ToIntFunction<T>、ToLongFunction<T>、ToDoubleFunction<T>
  • 类型:参数类型T,返回类型int long double
  • 用途:分别计算int long double值的函数
  1. IntFunction<R>、LongFunction<R>、DoubleFunction<R>
  • 类型:参数类型int long double,返回类型T
  • 用途:参数分别为int long double类型的函数

总结

以上就是Java8内置的核心接口,我们在使用的过程中,按需所取就ok啦。

相关文章

  • JAVA8函数式接口学习

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

  • java 各版本新特性介绍

    java8 新特性 Java8 主要包括的新特性有: 函数式接口如果一个接口只有一个抽象方法,那么该接口就成为一个...

  • 函数式接口

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

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

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

  • Lambda表达式

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

  • Java 8 Funciton、Consumer、Predica

    一、简述 Funciton、Consumer、Predicate是Java8中的新特性。他们都是函数式接口,位于j...

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

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

  • 浅谈函数式接口

    函数式接口是 Java8 引入的一个新特性,是一种特殊的接口:SAM类型的接口(Single Abstract M...

  • 常见面试题:java8有什么新特性?

    常见面试题:java8有什么新特性? 主要有以下这些新特性: lambda 表达式,经常配合函数式接口使用,可以有...

  • java8 新特性

    java8 新特性 核心内容 Lambda:函数式接口编程,简化代码量,类型上下文自动推断。 Stream Api...

网友评论

      本文标题:Java8的新特性--函数式接口

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