(原)
以前,在创建泛型时,是这么写的:
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