Java 8 新特性:3-函数(Function)接口

(原)

以前,在创建泛型时,是这么写的:

List<String> list =  new  ArrayList<String>();

现在,可以这么写了:

List<String> list = new ArrayList<>();

java8中,这种写法被叫作diamond语法,有些书里叫他钻石语法,有些则称之为菱形语法,说的就是这种语法。

 

看下面的例子:

package com.demo.jdk8;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;public class Test3 {public static void main(String[] args) {System.out.println("part1------------------");List<String> list = Arrays.asList("a","b","hello");list.stream().map(i -> i.toUpperCase()).forEach(i -> System.out.println(i));System.out.println("part2--------------------");list.stream().map(String::toUpperCase).forEach(i -> System.out.println(i));System.out.println("part3--------------------");Function<String , String> fun = String::toUpperCase;System.out.println(fun.apply("hello"));System.out.println("part4-------------------");System.out.println(calc1(3, v -> v+1));System.out.println("part5------------------");System.out.println(calc2(2, v -> v + 1, v -> v * 2));System.out.println("part6------------------");System.out.println(calc3(2, v -> v + 1, v -> v * 2));System.out.println("part7------------------");System.out.println(calc4(1,3,(v1,v2) -> v1 + v2));System.out.println("part8------------------");System.out.println(calc5(1,3,(v1,v2) -> v1 + v2 ,v -> v + 3) );}public static Integer calc1(Integer a ,Function<Integer, Integer> fun){return fun.apply(a);}public static Integer calc2(int a ,Function<Integer, Integer> fun1,Function<Integer, Integer> fun2){return  fun1.compose(fun2).apply(a);}public static Integer calc3(int a ,Function<Integer, Integer> fun1,Function<Integer, Integer> fun2){return  fun1.andThen(fun2).apply(a);}public static Integer calc4(int a ,int b ,BiFunction<Integer, Integer, Integer> biFun){return biFun.apply(a, b);}public static Integer calc5(int a ,int b ,BiFunction<Integer, Integer, Integer> biFun,Function<Integer, Integer> fun){return biFun.andThen(fun).apply(a, b);}
}

Java8里面,part1part2List上层Collection接口中,加入了一个stream方法

/*** Returns a sequential {@code Stream} with this collection as its source.** <p>This method should be overridden when the {@link #spliterator()}* method cannot return a spliterator that is {@code IMMUTABLE},* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}* for details.)*当分隔迭代器不能够返回一个不可变的,并发的或者延迟绑定值时,这个方法应该被重写。* @implSpec* The default implementation creates a sequential {@code Stream} from the* collection's {@code Spliterator}.*这个默认的实现会从Spliterator中创建一个串行流* @return a sequential {@code Stream} over the elements in this collection* @since 1.8*/default Stream<E> stream() {return StreamSupport.stream(spliterator(), false);}/*** Returns a possibly parallel {@code Stream} with this collection as its* source.  It is allowable for this method to return a sequential stream.** <p>This method should be overridden when the {@link #spliterator()}* method cannot return a spliterator that is {@code IMMUTABLE},* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}* for details.)** @implSpec* The default implementation creates a parallel {@code Stream} from the* collection's {@code Spliterator}.** @return a possibly parallel {@code Stream} over the elements in this* collection* @since 1.8*/default Stream<E> parallelStream() {return StreamSupport.stream(spliterator(), true);
}

stream方法会返回一个串行流,parallelStream方法会返回一个并行流,在这个例子中,你可以将它看作返回了一个Stream,这个Streamjava8新加的接口,其中有一个map方法。 

/*** Returns a stream consisting of the results of applying the given* function to the elements of this stream.*返回一个流,其中包含将给定函数应用到该流的元素的结果。* <p>This is an <a href="package-summary.html#StreamOps">intermediate* operation</a>.*这是一个中间操作* @param <R> The element type of the new stream* 返回一个新的流* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,*               <a href="package-summary.html#Statelessness">stateless</a>*               function to apply to each element* @return the new stream*/<R> Stream<R> map(Function<? super T, ? extends R> mapper);

这里的map接收一个参数,是一个Function接口

@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);

  这个方法的含义是接收一个流的参数,返回一个新的流。

在刚才的这个例子中list.stream().map(i -> i.toUpperCase()).forEach(i -> System.out.println(i));

list.stream()先将list转换成了stream,map是对Function接口的lambda实现,接收了每次迭代的一个参数,然后将该参数转换成大写再返回。

i -> i.toUpperCase()这里的i指的就是R apply(T t);这里的T,而i.toUpperCase()则是它的返回结果R,那String::toUpperCase这又是什么?在java8中,这叫方法的引用,因为toUpperCase并不是static方法,那么这个字符串必需是对象,那么这个对象是什么呢?其实这里的String就是传入的第一个参数T。这里总结一下,这个::后面的方法,一定是lambda表达式中第一个参数的方法。因为i -> i.toUpperCase()String::toUpperCase是等价,这第二种写法里,你也可以认为有一个string对象调用了toUpperCase,这个对象是谁呢?就是第一种写法里面的i。(这里确实不太好理解)

Function中,还有一个静态方法:

static <T> Function<T, T> identity() {return t -> t;
}

这是在java8中引入的新的写法。接口中可以有方法的实现,可以是default的,也可以是static的。

 

例子中的part3部分,用方法引用的方式创建了一个Function的实例。

part4calc方法中传递了二个参数,其中第二个参数实际上是传递了一个方法体,它其它可被称作为高阶函数,简单点讲就是可以把方法体当参数传入传出,这种方式在js中很常见。

 

另外,Function还有二个default函数。

 

/*** Returns a composed function that first applies the {@code before}* function to its input, and then applies this function to the result.* If evaluation of either function throws an exception, it is relayed to* the caller of the composed function.*首先应用before这个函数返回一个组合函数的结果,然后再返回这个函数的结果。*如果这个函数抛出异常,那么它会将这个异常交给调用这个函数的人处理。* @param <V> the type of input to the {@code before} function, and to the*           composed function* @param before the function to apply before this function is applied* @return a composed function that first applies the {@code before}* function and then applies this function* @throws NullPointerException if before is null** @see #andThen(Function)*/default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));
}/*** Returns a composed function that first applies this function to* its input, and then applies the {@code after} function to the result.* If evaluation of either function throws an exception, it is relayed to* the caller of the composed function.*首先运用这个函数的输入返回一个组合函数,然后运用这个after的参数返回结果* @param <V> the type of output of the {@code after} function, and of the*           composed function* @param after the function to apply after this function is applied* @return a composed function that first applies this function and then* applies the {@code after} function* @throws NullPointerException if after is null** @see #compose(Function)*/default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));
}

 

 

这个二函数的文档描述的有些绕,不是很好解理,但是看了它的源码实现以后再去看文档,这个描述确实挺恰当的。

要解理这二个默认方法的意思,首先还是得搞清楚R apply(T t);输入一个参数,返回一个结果。

compose(Function<? super V, ? extends T> before) 这个方法里面,首先调用了beforeapply方法,接收一个参数,返回一个结果,然后将返回的结果交给这个对象去执行apply方法,这个对象执行apply方法的参数就是这个before返回的结果,执行完后再返回一个结果。

andThen(Function<? super R, ? extends V> after) 这个方法恰好跟compose相反,首先执行这个对象的apply方法,然后把这个对象的结果交给after当作after的输入参数,它的返回值才是整个对象的结果。

结合上面demopart5part6部分,可以就能知道上面的结果是多少了。

Function是接收一个参数,返回一个结果,那有没有接收有二个参数,返回一个结果的呢?(JAVA只能返回一个结果!),当然有这个函数式接口叫BiFunction

 

@FunctionalInterface
public interface BiFunction<T, U, R> {/*** Applies this function to the given arguments.** @param t the first function argument* @param u the second function argument* @return the function result*/R apply(T t, U u);/*** Returns a composed function that first applies this function to* its input, and then applies the {@code after} function to the result.* If evaluation of either function throws an exception, it is relayed to* the caller of the composed function.** @param <V> the type of output of the {@code after} function, and of the*           composed function* @param after the function to apply after this function is applied* @return a composed function that first applies this function and then* applies the {@code after} function* @throws NullPointerException if after is null*/default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t, U u) -> after.apply(apply(t, u));}
}

 

文档就不解释了,如果Function你懂了,那么BiFunction你应该也能明白,只是这个函数只有andThen方法,没有composed方法,这是为什么呢?

因为刚才说了,所有的函数都可以接收多个参数,但是返回结果只有一个,拿andThen来说,首先会运算BiFunction这个对象的apply方法,只是这个方法只会产生一个结果,那么这个结果只能拿来给它的参数Function<? super R, ? extends V> after当参数用了,这就是为什么BiFunctionandThen方法参数为Function 而不是BiFunction了。

没有composed也是有原因的,因为composed需要先执行参数的方法,再执行BiFunction里的apply方法,但是由于这个结果只有一个值 ,是无法满足BiFunctionapply需要二个参数的条件,所以这里就并没有像Function一样,有一个composed方法。

例子请看这里:https://github.com/LeeScofield/java8

 

转载于:https://www.cnblogs.com/LeeScofiled/p/7051196.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-18499.html

如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网进行投诉反馈,一经查实,立即删除!


相关文章:

  • 通过js控制层的动态隐藏
  • iOS自动布局高级用法 纯代码约束写法
  • vue组件(Vue+webpack项目实战系列之三)
  • 6.20
  • 产业企业和投资机会研究 沈阳新松机器人自动化股份有限公司(300024)
  • idea整个项目乱码解决办法
  • 从头认识java-16.4 nio的读与写(ByteBuffer的使用)
  • 宿命的PSS
  • 07_上午内容回顾
  • python三维可视化:配置tvtk
  • working copy is not up-to-date
  • 1.strcpy使用注意
  • html中设置锚点定位的几种常见方法
  • elasticsearch.net search入门使用指南中文版(翻译)
  • 重写( override)and 重载(overload)
  • 【java面试题】equals()方法和==的比较区别?
  • 合并出错:svn Working copy and merge source not ready for reintegration
  • 阿里笔试题:求两个子序列的最大连续子序列
  • 自己手写WEB程序框架并执行
  • Django知识总结
  • Android学习笔记(十二)——使用意图传递数据的几种方式
  • ERP概念介绍
  • LibreOJ β Round #2 E. 数论只会 GCD
  • [bzoj1566][NOI2009]管道取珠
  • 46.Android 自己定义Dialog
  • php在IIS上put,delete请求报404
  • ui-sref与$state.go()的使用
  • Android7.0无需FileProvide搞定URI拍照、应用安装问题
  • SEO外链优化
  • [POJ1952]BUY LOW, BUY LOWER
  • 前端特效(css3)
  • 重新复习基础草稿:迭代器的使用
  • PHP第一天
  • tomcat 实现域名crm.test.com訪问
  • C# .Net ListT中Remove()、RemoveAt()、RemoveRange()、RemoveAll()的区别,ListT删除汇总
  • Win10系统80端口被系统进程占用
  • Git最基本入门,只是个感想总结啊啊啊不要搜到我0.0
  • Jmeter之旅
  • 2015 HIAST Collegiate Programming Contest C
  • raft Paxos
  • LINUX中错误 SELinux is disabled
  • 如何在同一Linux服务器上创建多站点
  • Tensorflow自编码器及多层感知机
  • 陈梓涵:关于编程的胡扯
  • LoaderManager使用具体解释(四)---实例:AppListLoader
  • python之路——作业:Select FTP(仅供参考)
  • T7315 yyy矩阵折叠(长)
  • 常用的断言总结
  • pathway一些网站
  • c++ primer plus(第6版)中文版 第十二章编程练习答案