Stream是Java 8新增的接口,Stream能够认为是一个高级版本的 Iterator。它表明着数据流,流中的数据元素的数量能够是有限的,也能够是无限的。java
Stream跟Iterator的差异是编程
Stream的操做是创建在函数式接口的组合之上的。Java8中新增的函数式接口都在java.util.function包下。这些函数式接口能够有多种分类方式。app
Function是从T到R的一元映射函数。将参数T传递给一个函数,返回R。即R = Function(T)less
@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);
/** * 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. * * @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. * * @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));
}
/** * Returns a function that always returns its input argument. * * @param <T> the type of the input and output objects to the function * @return a function that always returns its input argument */
static <T> Function<T, T> identity() {
return t -> t;
}
}
复制代码
Function默认实现了3个default方法,分别是compose、andThen和identity。dom
方法名 | 对应函数 | 描述 |
---|---|---|
compose | V=Function(ParamFunction(T)) | 它体现了嵌套关系 |
andThen | V= ParamFunction(Function(T)) | 转换了嵌套的顺序 |
identity | Function(T)=T | 传递自身的函数调用 |
compose和andThen对于两个函数f和g来讲,f.compose(g)等价于g.andThen(f)。ide
Predicate是一个谓词函数,主要做为一个谓词演算推导真假值存在,返回布尔值的函数。Predicate等价于一个Function的boolean型返回值的子集。函数式编程
@FunctionalInterface
public interface Predicate<T> {
/** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */
boolean test(T t);
/** * Returns a composed predicate that represents a short-circuiting logical * AND of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code false}, then the {@code other} * predicate is not evaluated. * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * @return a composed predicate that represents the short-circuiting logical * AND of this predicate and the {@code other} predicate * @throws NullPointerException if other is null */
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
/** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */
default Predicate<T> negate() {
return (t) -> !test(t);
}
/** * Returns a composed predicate that represents a short-circuiting logical * OR of this predicate and another. When evaluating the composed * predicate, if this predicate is {@code true}, then the {@code other} * predicate is not evaluated. * * <p>Any exceptions thrown during evaluation of either predicate are relayed * to the caller; if evaluation of this predicate throws an exception, the * {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * @return a composed predicate that represents the short-circuiting logical * OR of this predicate and the {@code other} predicate * @throws NullPointerException if other is null */
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
/** * Returns a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)}. * * @param <T> the type of arguments to the predicate * @param targetRef the object reference with which to compare for equality, * which may be {@code null} * @return a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)} */
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
复制代码
Predicate的默认方法是and、negate、or。函数
Consumer是从T到void的一元函数,接受一个入参但不返回任何结果的操做。ui
@FunctionalInterface
public interface Consumer<T> {
/** * Performs this operation on the given argument. * * @param t the input argument */
void accept(T t);
/** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
复制代码
Consumer的默认方法是andThen。this
Supplier是表示结果的供应者。
@FunctionalInterface
public interface Supplier<T> {
/** * Gets a result. * * @return a result */
T get();
}
复制代码
Supplier的用法:
Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return "hello suppiler";
}
};
System.out.println(supplier.get());
复制代码
或者:
Supplier<User> userSupplier = User::new;
userSupplier.get(); // new User
复制代码
Java 8新增了CompletableFuture,它的不少方法的入参都用到了Supplier。
Java 8有多种方式来建立Stream:
其实最终都是依赖底层的StreamSupport类来完成Stream建立。
中间操做又能够分为无状态的(Stateless)和有状态的(Stateful),无状态中间操做是指元素的处理不受前面元素的影响,而有状态的中间操做必须等到全部元素处理以后才知道最终结果。
Stream的中间操做只是一种标记,只有执行告终束操做才会触发实际计算。 熟悉RxJava、Scala的同窗能够看到,Stream中间操做的各个方法在RxJava、Scala中均可以找到熟悉的身影。
短路操做是指不用处理所有元素就能够返回结果。短路操做必须一个元素处理一次。
非短路操做能够批量处理数据,可是须要处理彻底部元素才会返回结果。
在建立Stream时,默认是建立串行流。可是可使用parallelStream()来建立并行流或者parallel()将串行流转换成并行流。并行流也能够经过sequential()转换成串行流。
Java 8 Stream的并行流,本质上仍是使用Fork/Join模型。
在Java开发中,若是使用了Java 8,那么强烈建议使用Stream。由于Stream的每一个操做均可以依赖Lambda表达式,它是一种声明式的数据处理方式,而且Stream提升了数据处理效率和开发效率。