2016-04-11  java 

Java Stream API examples (forEach, map)

forEach

import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        Arrays.asList("a", "b", "c", "d", "e")
            .stream()
            .forEach(s -> System.out.println(s));
    }
}
$ javac -Xdiags:verbose Main.java
$ java Main
a
b
c
d
e

map

import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        Arrays.asList("a", "b", "c", "d", "e")
            .stream()
            .map(s -> s + s + s + s + s)
            .forEach(s -> System.out.println(s));
    }
}
$ javac -Xdiags:verbose Main.java
$ java Main
aaaaa
bbbbb
ccccc
ddddd
eeeee

参照

 2016-04-11  java