Java - Comment convertir un tableau en flux

Java - Comment convertir un tableau en flux

Dans Java 8, vous pouvez utiliserArrays.stream ouStream.of pour convertir un tableau en un flux.

1. Tableaux d'objets

Pour les tableaux d'objets,Arrays.stream etStream.of renvoient la même sortie.

TestJava8.java

package com.example.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 stream1 = Arrays.stream(array);
        stream1.forEach(x -> System.out.println(x));

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

}

Sortie

a
b
c
d
e
a
b
c
d
e

Vérifiez le code source JDK.

Arrays.java

   /**
     * Returns a sequential {@link Stream} with the specified array as its
     * source.
     *
     * @param  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  Stream stream(T[] array) {
        return stream(array, 0, array.length);
    }

Stream.java

   /**
     * Returns a sequential ordered stream whose elements are the specified values.
     *
     * @param  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 Stream of(T... values) {
        return Arrays.stream(values);
    }

Note
Pour les tableaux d'objets, la méthodeStream.of appelle lesArrays.stream en interne.

2. Tableaux Primitifs

Pour un tableau primitif, lesArrays.stream etStream.of renverront une sortie différente.

TestJava8.java

package com.example.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
        Stream temp = Stream.of(intArray);

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

    }

}

Sortie

1
2
3
4
5
1
2
3
4
5

Vérifiez le code source 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  the type of stream elements
     * @return a singleton sequential stream
     */
    public static Stream of(T t) {
        return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
    }

Which one?
Pour les tableaux d'objets, les deux appellent les mêmesArrays.stream (voir l'exemple 1, code source JDK). Pour les tableaux primitifs, je préfère aussiArrays.stream, car il renvoie directement desIntStream de taille fixe, plus facile à manipuler.

P.S Tested with Oracle JDK 1.8.0_77