Adam Bien's Weblog

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.


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices

Newsletter: airhacks.news

A book about rethinking Java EE Patterns

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
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License