(原)

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

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里面,part1、part2中List上层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,这个Stream是java8新加的接口,其中有一个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的实例。

part4在calc方法中传递了二个参数,其中第二个参数实际上是传递了一个方法体,它其它可被称作为高阶函数,简单点讲就是可以把方法体当参数传入传出,这种方式在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) 这个方法里面,首先调用了before的apply方法,接收一个参数,返回一个结果,然后将返回的结果交给这个对象去执行apply方法,这个对象执行apply方法的参数就是这个before返回的结果,执行完后再返回一个结果。

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

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

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当参数用了,这就是为什么BiFunction的andThen方法参数为Function 而不是BiFunction了。

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

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

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

Java 8 新特性:3-函数(Function)接口相关推荐

  1. Java JDK1.8新特性之四大函数式接口

    JDK 1.8的一些新特性 四大核心函数式接口(Consumer.Predicate.Supplier.Function),结合lambda表达式 import java.util.ArrayList ...

  2. 跟我学 Java 8 新特性之 Stream 流(六)收集

    转载自   跟我学 Java 8 新特性之 Stream 流(六)收集 我们前面的五篇文章基本都是在说将一个集合转成一个流,然后对流进行操作,其实这种操作是最多的,但有时候我们也是需要从流中收集起一些 ...

  3. 跟我学 Java 8 新特性之 Stream 流(三)缩减操作

    转载自   跟我学 Java 8 新特性之 Stream 流(三)缩减操作 和前面两篇文章一起服用,效果会更佳.通过对流API的基础体验Demo和关键知识点的讲解,相信大家对流API都有一定的认识了, ...

  4. Java 8 新特性 lambda表达式

    / Created by Manager on 2021/4/1. Java 8 新特性 lambda表达式 StreamAPI 新日期 新注解 */ 视频连接 1https://www.bilibi ...

  5. Java 8 新特性 宋红康跟学

    Java 8 新特性 Java 8 新特性简介 思维导图 并行流与串行流 Lambda表达式 Lambda 表达式语法 语法格式一:无参,无返回值,Lambda体只需一条语句 语法格式二:Lambda ...

  6. java新版本新特性

    2. Java8新特性:Lambda表达式 2.1 关于Java8新特性简介 Java 8 (又称为 JDK 8或JDK1.8) 是 Java 语言开发的一个主要版本. Java 8 是oracle公 ...

  7. Java 8 新特性之Lambda

    Java 8 新特性之Lambda.Stream.Optional Java 8 新特性之Lambda.Stream.Optional 速度更快 代码更少(增加了新的语法Lambda表达式) 强大的S ...

  8. 跟我学 Java 8 新特性之 Stream 流基础体验

    转载自   跟我学 Java 8 新特性之 Stream 流基础体验 Java8新增的功能中,要数lambda表达式和流API最为重要了.这篇文章主要介绍流API的基础,也是流API系列的第一篇文章, ...

  9. 跟我学 Java 8 新特性之 Stream 流(二)关键知识点

    转载自   跟我学 Java 8 新特性之 Stream 流(二)关键知识点 我们的第一篇文章,主要是通过一个Demo,让大家体验了一下使用流API的那种酣畅淋漓的感觉.如果你没有实践,我还是再次呼吁 ...

  10. 跟我学 Java 8 新特性之 Stream 流(四)并行流

    转载自   跟我学 Java 8 新特性之 Stream 流(四)并行流 随着对流API认识的慢慢深入,本章我们要讨论的知识点是流API里面的并行流了. 在开始讨论并行流之前,我先引发一下大家的思考, ...

最新文章

  1. cmd文件内容添加到文件内容命令
  2. 转:测试部工作不受重视怎么办?
  3. Android之EasyPermissions源码解析
  4. 前端测试 jest 中判断函数由来
  5. [改善Java代码]不要主动进行垃圾回收
  6. Android java 多线程(三)
  7. leading dimension
  8. 再见,2014;你好2015
  9. 八杯水微博程序演示及下载
  10. python曲线拟合预测_用python做曲线拟合
  11. 25k英里高速建48个充电走廊,美国电动汽车产业迎来春天
  12. python apk fr_Python fr包_程序模块 - PyPI - Python中文网
  13. Spark重要概念提出时间戳和原因
  14. 计算机系统通过执行通道程序完成数据,计算机系统结构_第四章练习 答案
  15. 灰度世界 matlab,灰度世界算法(Gray World Algorithm)和White Patch Retinex算法
  16. restclient发送json_如何使用restclient来发送post请求参数
  17. arcgis栅格数据绘制等值线_ArcGIS教程:绘制等值线的工作原理
  18. 夜神模拟器 Nox Player 雷电模拟器 掉线 连不上 运行不显示的解决方案
  19. 2.Apache服务器配置(Ubuntu)
  20. 猜数字小游戏(随机生成’三剑客‘)

热门文章

  1. python从入门到实践第二版_【Python入门教程】第二季网络爬虫基础
  2. linux无线adb,linux 无法连接adb 设备
  3. linux修改ip配置文件_SSH连接Linux主机进行开发
  4. mysql自带计划任务
  5. 在项目中使用HTMLDom的事件冒泡机制
  6. 浅析 @PathVariable 和 @RequestParam(转发,非原创)
  7. Vuex之理解Modules
  8. htons htonl ntohl ntohs 的区别和作用
  9. 软件应用:HexorBase Tool 实战测试!
  10. HTML 4.0 语 法 教 学