温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JDK8新特性java.util.function-Function接口怎么使用

发布时间:2023-05-06 11:22:00 来源:亿速云 阅读:84 作者:iii 栏目:开发技术

这篇文章主要介绍“JDK8新特性java.util.function-Function接口怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“JDK8新特性java.util.function-Function接口怎么使用”文章能帮助大家解决问题。

JDK8新特性-java.util.function-Function接口

14年,Oracle公司如期发布了Java 8正式版。现如今4年过去了,终于鼓起勇气认真对待它,就好似虽然认识了好几年的伙伴,突然感觉要成为情侣的感觉……

JDK 1.8 API包含了很多内建的函数式接口,在老Java中常用到的比如Comparator或者Runnable接口,这些接口都增加了@FunctionalInterface注解以便能用在lambda上。

现如今,我们则从Function常用函数入口,真正了解一下。

nametypedescription
ConsumerConsumer< T >接收T对象,不返回值
PredicatePredicate< T >接收T对象并返回boolean
FunctionFunction< T, R >接收T对象,返回R对象
SupplierSupplier< T >提供T对象(例如工厂),不接收值
UnaryOperatorUnaryOperator接收T对象,返回T对象
BinaryOperatorBinaryOperator接收两个T对象,返回T对象

标注为FunctionalInterface的接口被称为函数式接口,该接口只能有一个自定义方法,但是可以包括从object类继承而来的方法。

如果一个接口只有一个方法,则编译器会认为这就是一个函数式接口。

是否是一个函数式接口,需要注意的有以下几点:

  • 该注解只能标记在”有且仅有一个抽象方法”的接口上。

  • JDK8接口中的静态方法和默认方法,都不算是抽象方法。

  • 接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。

  • 该注解不是必须的,如果一个接口符合”函数式接口”定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。

  • 在一个接口中定义两个自定义的方法,就会产生Invalid &lsquo;@FunctionalInterface&rsquo; annotation; FunctionalInterfaceTest is not a functional interface错误.

Function常用方法&&实践

//将Function对象应用到输入的参数上,然后返回计算结果。
R apply(T t);
//返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
//返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

compose 和 andThen 的不同之处是函数执行的顺序不同。

compose 函数先执行参数,然后执行调用者,而 andThen 先执行调用者,然后再执行参数。

public static void main(String[] args) {
        Function<Integer, Integer> name = e -> e * 2;
        Function<Integer, Integer> square = e -> e * e;
        int value = name.andThen(square).apply(3);
        System.out.println("andThen value=" + value);
        int value2 = name.compose(square).apply(3);
        System.out.println("compose value2=" + value2);
        //返回一个执行了apply()方法之后只会返回输入参数的函数对象
        Object identity = Function.identity().apply("huohuo");
        System.out.println(identity);
    }

直接看结果:

andThen value=36
compose value2=18
huohuo

apply基本应用

字符串长度记录返回

public class MyFunction implements Function<String,Integer>{

    @Override
    public Integer apply(String s) {
        return s.length();
    }
}

返回两个字符串的连接,BiFunction与Function的不同就是传入两个参数,依旧返回一个值。

public class MyBiFunction implements BiFunction<String, String, String> {
    @Override
    public String apply(String s, String s2) {
        return s+";"+s2;
    }
}

最后调用结果:

private static String hello = "Nice to meet you";
    private static String name = "my name is huohuo";

    public static void main(String[] args) {
        MyFunction myFunction = new MyFunction();
        MyBiFunction biFunction = new MyBiFunction();
        int num = myFunction.apply(hello);
        String valueBi = biFunction.apply(hello, name);
        //hello长度返回
        System.out.println(num);
        //语句整合返回
        System.out.println(valueBi);
    }

返回值:

16
Nice to meet you;my name is huohuo

其实使用的过程感觉这些都无所必要,但是对于新特性以及代码规范而言,即使是简易代码,也有了一个整合的过程。

Function简单实践仅此为止,下篇文章讲述Predicate的使用。

关于“JDK8新特性java.util.function-Function接口怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jdk
AI