Java – 怎样将 Array 转为 Stream

   在java 8中,您可以使用arrays.stream或Stream.of将 Arrays 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

检查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.stream 和Stream.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 -> 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);
 }
选择哪一个?
对象的数组,都是调用同一个arrays.stream(参见例1,JDK源代码)。原始数组,我更喜欢arrays.stream为好,因为它返回固定大小的intstream,容易操作。

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值