JSRUN 用代码说话

Java – 将 Array 转成 Stream

编辑教程

在Java 8里,你可以用Arrays.streamStream.of来将数组转换成流。

1. 对象数组

对于对象数组,Arrays.streamStream.of方法都可以返回同样的输出结果。

TestJava8.java

package com.mkyong.java8;

import java.util.Arrays;
import java.util.stream.Stream;

public class TestJava8 {

    public static void main(String[] args) {

        String[] array = {"a", "b", "c", "d", "e"};

        //Arrays.stream
        Stream<string> stream1 = Arrays.stream(array);
        stream1.forEach(x -&gt; System.out.println(x));

        //Stream.of
        Stream<string> stream2 = Stream.of(array);
        stream2.forEach(x -&gt; System.out.println(x));
    }

}

Output

a
b
c
d
e
a
b
c
d
e

让我们看一下JDK的源码。

Arrays.java

/**
     * Returns a sequential {@link Stream} with the specified array as its
     * source.
     *
     * @param <t> The type of the array elements
     * @param array The array, assumed to be unmodified during use
     * @return a {@code Stream} for the array
     * @since 1.8
     */
    public static <t> Stream<t> stream(T[] array) {
        return stream(array, 0, array.length);
    }

Stream.java

/**
     * Returns a sequential ordered stream whose elements are the specified values.
     *
     * @param <t> the type of stream elements
     * @param values the elements of the new stream
     * @return the new stream
     */
    @SafeVarargs
    @SuppressWarnings("varargs") // Creating a stream from an array is safe
    public static<t> Stream<t> of(T... values) {
        return Arrays.stream(values);
    }

请注意
对于对象数组,Stream.of方法会在内部调用Arrays.stream

2. 原始数组

对于原始数组,Arrays.streamStream.of会返回不同的输出结果。

TestJava8.java

package com.mkyong.java8;

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class TestJava8 {

    public static void main(String[] args) {

        int[] intArray = {1, 2, 3, 4, 5};

        // 1. Arrays.stream -&gt; IntStream 
        IntStream intStream1 = Arrays.stream(intArray);
        intStream1.forEach(x -&gt; System.out.println(x));

        // 2. Stream.of -&gt; Stream<int[]>
        Stream<int[]> temp = Stream.of(intArray);

        // Cant print Stream<int[]> directly, convert / flat it to IntStream 
        IntStream intStream2 = temp.flatMapToInt(x -&gt; Arrays.stream(x));
        intStream2.forEach(x -&gt; System.out.println(x));

    }

}

Output

1
2
3
4
5
1
2
3
4
5

让我们看一下JDK的源码。

Arrays.java

/**
     * Returns a sequential {@link IntStream} with the specified array as its
     * source.
     *
     * @param array the array, assumed to be unmodified during use
     * @return an {@code IntStream} for the array
     * @since 1.8
     */
    public static IntStream stream(int[] array) {
        return stream(array, 0, array.length);
    }

Stream.java

/**
     * Returns a sequential {@code Stream} containing a single element.
     *
     * @param t the single element
     * @param <t> the type of stream elements
     * @return a singleton sequential stream
     */
    public static<t> Stream<t> of(T t) {
        return StreamSupport.stream(new Streams.StreamBuilderImpl&lt;&gt;(t), false);
    }

该用哪一个呢?
对于对象数组,都会调用Arrays.stream(参见示例1,JDK源代码)。 对于原始数组,更推荐Arrays.stream,因为它直接返回固定大小的IntStream,更易于操作。

P.S 使用Oracle JDK 1.8.0_77版本测试

References

  1. Arrays JavaDoc
  2. Stream JavaDoc
  3. How to print an array, java and java 8 examples
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。 大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
支付宝
9.99
无法付款,请点击这里
金额: 0
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟