Languages
[Edit]
EN

Java 8 - create stream from existing array

0 points
Created by:
Inferio
328

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

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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