java– How to convert Array to Stream__java

Source: Internet
Author: User

In Java 8, you can use Arrays.stream or stream.of to move the 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

Check 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 the array, assumed to is unmodified during US E
  * @return a {@code Stream} for the array
  * @since 1.8 */public
 static <T> stream<t> str EAM (t[] array) {return
     stream (array, 0, array.length);
 }
Stream.java
/**
  * Returns a sequential ordered stream whose elements the are 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
The Stream.of method of the object array is called inside the Arrays.stream. 2. Original Array

For the original array, different output results are returned with Arrays.stream and 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 (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 is unmodified during use
  * @return a {@code intstream} for the ARRAY
  * @since 1.8
  */public
 static Intstream stream (int[] array) {return
     stream (array, 0, array.length); c19/>}
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 Sequentia L Stream
 /public static<t> stream<t> of (T-t) {return
     Streamsupport.stream (new Streams.streambuilderimpl<> (t), false);
 
Which one to choose.
An array of objects, all of which call the same arrays.stream (see example 1,JDK Source code). The original array, I prefer arrays.stream as good, because it returns a fixed size of intstream, easy to operate.

P.S tested with Oracle JDK 1.8.0_77 References Arrays Javadoc Stream Javadoc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.