adam bien's blog

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.