Java8 Stream 的总结

本贴最后更新于 2225 天前,其中的信息可能已经时移世异

一. Stream 的特性

Stream 是 Java 8 新增的接口,Stream 可以认为是一个高级版本的 Iterator。它代表着数据流,流中的数据元素的数量可以是有限的,也可以是无限的。

Stream 跟 Iterator 的差别是

  • 无存储:Stream 是基于数据源的对象,它本身不存储数据元素,而是通过管道将数据源的元素传递给操作。
  • 函数式编程:对 Stream 的任何修改都不会修改背后的数据源,比如对 Stream 执行 filter 操作并不会删除被过滤的元素,而是会产生一个不包含被过滤元素的新的 Stream。
  • 延迟执行:Stream 的操作由零个或多个中间操作(intermediate operation)和一个结束操作(terminal operation)两部分组成。只有执行了结束操作,Stream 定义的中间操作才会依次执行,这就是 Stream 的延迟特性。
  • 可消费性:Stream 只能被“消费”一次,一旦遍历过就会失效。就像容器的迭代器那样,想要再次遍历必须重新生成一个新的 Stream。

二. Java 8 新增的函数式接口

Stream 的操作是建立在函数式接口的组合之上的。Java8 中新增的函数式接口都在 java.util.function 包下。这些函数式接口可以有多种分类方式。

2.1 Function

Function 是从 T 到 R 的一元映射函数。将参数 T 传递给一个函数,返回 R。即 R = Function(T)

@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。

方法名 对应函数 描述
compose V=Function(ParamFunction(T)) 它体现了嵌套关系
andThen V= ParamFunction(Function(T)) 转换了嵌套的顺序
identity Function(T)=T 传递自身的函数调用

compose 和 andThen 对于两个函数 f 和 g 来说,f.compose(g)等价于 g.andThen(f)。

2.2 Predicate

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。

2.3 Consumer

Consumer 是从 T 到 void 的一元函数,接受一个入参但不返回任何结果的操作。

@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。

2.4 Supplier

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。

三. Stream 用法

3.1 Stream 的创建

Java 8 有多种方式来创建 Stream:

  • 通过集合的 stream()方法或者 parallelStream()
  • 使用流的静态方法,比如 Stream.of(Object[]), IntStream.range(int, int) 或者 Stream.iterate(Object, UnaryOperator)。
  • 通过 Arrays.stream(Object[])方法。
  • BufferedReader.lines()从文件中获得行的流。
  • Files 类的操作路径的方法,如 list、find、walk 等。
  • 随机数流 Random.ints()。
  • 其它一些类提供了创建流的方法,如 BitSet.stream(), Pattern.splitAsStream(java.lang.CharSequence), 和 JarFile.stream()。

其实最终都是依赖底层的 StreamSupport 类来完成 Stream 创建。

3.2 中间操作

中间操作又可以分为无状态的(Stateless)和有状态的(Stateful),无状态中间操作是指元素的处理不受前面元素的影响,而有状态的中间操作必须等到所有元素处理之后才知道最终结果。

Stream 的中间操作只是一种标记,只有执行了结束操作才会触发实际计算。 熟悉 RxJava、Scala 的同学可以看到,Stream 中间操作的各个方法在 RxJava、Scala 中都可以找到熟悉的身影。

3.3 结束操作

3.3.1 短路操作

短路操作是指不用处理全部元素就可以返回结果。短路操作必须一个元素处理一次。

3.3.1 非短路操作

非短路操作可以批量处理数据,但是需要处理完全部元素才会返回结果。

四. 并行流

在创建 Stream 时,默认是创建串行流。但是可以使用 parallelStream()来创建并行流或者 parallel()将串行流转换成并行流。并行流也可以通过 sequential()转换成串行流。

Java 8 Stream 的并行流,本质上还是使用 Fork/Join 模型。

五. 总结

在 Java 开发中,如果使用了 Java 8,那么强烈建议使用 Stream。因为 Stream 的每个操作都可以依赖 Lambda 表达式,它是一种声明式的数据处理方式,并且 Stream 提高了数据处理效率和开发效率。

作者:Tony 沈哲
链接:https://juejin.im/post/5a8a71506fb9a0633d71e2c9
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3165 引用 • 8206 回帖 • 1 关注
  • 后台
    7 引用 • 39 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...