Java 8: Converting An Array To A Stream

The Java 8 Arrays class allows a convenient conversion between an array and a Stream:


import java.util.Arrays;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;

public class ArraysToStreamTest {

    @Test
    public void arraysToStream() {
        String languages[] = {"java", "javascript"};
        Stream<String> stream = Arrays.stream(languages);

        long count = stream.filter(l -> l.endsWith("va")).count();
        assertThat(count, is(1l));
    }


}

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

What's the difference with Stream.of(...) ?

Posted by AlexThep on January 01, 2017 at 12:31 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License