(原)
以前,在创建泛型时,是这么写的:
List list = new ArrayList();
现在,可以这么写了:
List 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.Function;
public class Test3 {
public static void main(String[] args) {
List list = Arrays.asList("a","b","hello");
list.stream().map(i -> i.toUpperCase()).forEach(i -> System.out.println(i));
System.out.println("--------------------");
list.stream().map(String::toUpperCase).forEach(i -> System.out.println(i));
System.out.println("--------------------");
Function fun = String::toUpperCase;
System.out.println(fun.apply("hello"));
}
}
Java8里面,List上层Collection接口中,加入了一个stream方法
/**
* Returns a sequential [email protected] Stream} with this collection as its source.
*
*
This method should be overridden when the [email protected] #spliterator()}
* method cannot return a spliterator that is [email protected] IMMUTABLE},
* [email protected] CONCURRENT}, or late-binding. (See [email protected] #spliterator()}
* for details.)
*当分隔迭代器不能够返回一个不可变的,并发的或者延迟绑定值时,这个方法应该被重写。
* @implSpec
* The default implementation creates a sequential [email protected] Stream} from the
* collection‘s [email protected] Spliterator}.
*这个默认的实现会从Spliterator中创建一个串行流
* @return a sequential [email protected] Stream} over the elements in this collection
* @since 1.8
*/
default Stream stream() {
return StreamSupport.stream(spliterator(), false);
}
/**
* Returns a possibly parallel [email protected] Stream} with this collection as its
* source. It is allowable for this method to return a sequential stream.
*
*
This method should be overridden when the [email protected] #spliterator()}
* method cannot return a spliterator that is [email protected] IMMUTABLE},
* [email protected] CONCURRENT}, or late-binding. (See [email protected] #spliterator()}
* for details.)
*
* @implSpec
* The default implementation creates a parallel [email protected] Stream} from the
* collection‘s [email protected] Spliterator}.
*
* @return a possibly parallel [email protected] Stream} over the elements in this
* collection
* @since 1.8
*/
default Stream 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.
*返回一个流,其中包含将给定函数应用到该流的元素的结果。
*
This is an intermediate
* operation.
*这是一个中间操作
* @param The element type of the new stream
* 返回一个新的流
* @param mapper a non-interfering,
* function to apply to each element
* @return the new stream
*/
Stream map(Function super T, ? extends R> mapper);
这里的map接收一个参数,是一个Function接口
@FunctionalInterface
public interface Function
这是一个函数式接口,其中有一个抽象方法。
/**
* 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 Function identity() {
return t -> t;
}
这是在java8中引入的新的写法。接口中可以有方法的实现,可以是default的,也可以是static的。
原文:http://www.cnblogs.com/LeeScofiled/p/7051196.html