Created
September 19, 2015 13:22
-
-
Save mmts1007/a37e6fba2aafde4cf230 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class HelloLambda { | |
public static void main(String... args) { | |
List<String> names = new ArrayList<>(); | |
names.add("sample 1"); | |
names.add("sample 2"); | |
names.add("sample 3"); | |
// Java 7 での書き方 | |
for (String name : names) { | |
System.out.println(name); | |
} | |
// Java 8 での書き方 | |
names.stream().forEach((String name) -> { | |
System.out.println(name); | |
}); | |
// 処理が 1行しか無いので、波括弧は省略可能 | |
names.stream().forEach((String name) -> System.out.println(name)); | |
// さらにメソッドの引数は、Stream から推論可能(リストが String型)なため省略可能 | |
names.stream().forEach((name) -> System.out.println(name)); | |
// 引数が 1つの場合は丸括弧省略可能 | |
names.stream().forEach(name -> System.out.println(name)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment