转载 

Java – How to convert Array to Stream

分类:    230人阅读    IT小君  2018-07-01 09:41

In Java 8, you can either use Arrays.stream or Stream.of to convert an Array into a Stream.

1. Object Arrays

For object arrays, both Arrays.stream and Stream.of returns the same output.

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 -> System.out.println(x));

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

}

Output

a
b
c
d
e
a
b
c
d
e

Review the JDK source code.

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);
    }
Note
For object arrays, the Stream.of method is calling the Arrays.stream internally.

2. Primitive Arrays

For primitive array, the Arrays.stream and Stream.of will return different output.

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 -> IntStream 
        IntStream intStream1 = Arrays.stream(intArray);
        intStream1.forEach(x -> System.out.println(x));

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

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

    }

}

Output

1
2
3
4
5
1
2
3
4
5

Review the JDK source code.

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<>(t), false);
    }
Which one?
For object arrays, both are calling the same Arrays.stream (refer example 1, JDK source code). For primitive arrays, I prefer Arrays.stream as well, because it returns fixed size IntStream directly, easier to manipulate it.

P.S Tested with Oracle JDK 1.8.0_77

References

  1. Arrays JavaDoc
  2. Stream JavaDoc
  3. How to print an array, java and java 8 examples
点击广告,支持我们为你提供更好的服务

html5 svg夜空中星星流星动画场景特效

css+js实现的颜色渐变数字时钟动画特效

网页设计开发公司网站模板

html5图标下拉搜索框自动匹配代码

HTML5数字产品服务公司网站模板

html5 canvas彩色碎片组合球形旋转动画特效

HTML5现代家居装潢公司网站模板

jQuery右端悬浮带返回顶部特效

js+css3抽奖转盘旋转点餐代码

响应式太阳能能源公司网站模板

响应式咖啡饮品宣传网站模板

HTML5 Canvas竖直流动线条背景动画特效

中小型创意设计服务公司网站模板

响应式时尚单品在线商城网站模板

有机水果蔬菜HTML5网站模板

小众时尚单品在线电子商务网站模板

css鼠标跟随文字模糊特效

canvas炫酷鼠标移动文字粒子特效

现代时尚家具公司网站模板

html5 canvas进度条圆环图表统计动画特效

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务