Languages
[Edit]
EN

Java 8 - create stream from existing array

0 points
Created by:
Inferio
268

In this article, we would like to show you how to create a stream from an existing array in Java 8.

Quick solution:

String[] letters = new String[]{"A", "B", "C"};

// create stream from array
Stream<String> streamOfLetters = Arrays.stream(letters);

 

Practical example

In this example, we use Arrays.stream() to create a stream from the existing array.

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

public class Example {

    public static void main(String[] args) {
        String[] letters = new String[]{"A", "B", "C"};

        // create stream from array
        Stream<String> streamOfLetters = Arrays.stream(letters);

        // print elements
        streamOfLetters.forEach(System.out::println);
    }
}

Output:

A
B
C

Related posts

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join