2. Java8 (JSR 337)
JSR 308: Annotations on Java Types
JSR 310: Date and Time API
JSR TBD: More Small Enhancements to the Java
Programming Language
JSR 335: Lambda Expressions for the Java Programming
Language
JSR TBD: Java Platform Module System
3. 例1 Before
File srcDir = new File(“src");
File[] javaFiles = srcDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".java");
}
});
4. 例1 After
File srcDir = new File(“src");
File[] javaFiles = srcDir.listFiles(
(dir, name) -> name.endsWith(".java"));
5. 例2 Before
static final List<Integer> NUMBER_LIST =
Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> evenNumberList =
new ArrayList<Integer>();
for (int number : NUMBER_LIST) {
if (number % 2 == 0) {
evenNumberList.add(number);
}
}
6. 例2 After
static final List<Integer> NUMBER_LIST =
Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> evenNumberList =
new ArrayList<Integer>(NUMBER_LIST);
evenNumberList.retainAll(i -> i % 2 == 0);
28. Method references
• ラムダ式を書く代わりに既存のメソッドを
呼び出すことができる
• 形式
o <クラス名 or インスタンス名> :: <メソッド名 or
new>
• 例
o System::getProperty
o "abc"::length
o String::length
o ArrayList::new
29. 例1 static メソッド
class Person {
private final String name;
private final int age;
public static int compareByAge(Person a, Person b) { ... }
public static int compareByName(Person a, Person b)
{ ... }
}
Person[] people = ...
Arrays.sort(people, Person::compareByAge);
30. 例2 インスタンスメソッド(1)
(<インスタンス名>::<メソッド名>)
class ComparisonProvider {
public int compareByAge(Person a, Person b) { ... }
public int compareByName(Person a, Person b) { ... }
}
ComparisonProvider comparisonProvider =
new ComparisonProvider();
Arrays.sort(
people,
comparisonProvider ::compareByName);
31. 例3 インスタンスメソッド(2)
(<クラス名>::<メソッド名>)
String[] names = new String[] {"Hoge", "Piyo", "Boo"};
Arrays.sort(names, String::compareToIgnoreCase);
class String {
public int compareToIgnoreCase(String str)
}
interface Comparator<T> {
int compare(T o1, T o2);
}
32. 例4 コンストラクタ
class Car { ... }
interface CarFactory {
Car newCar();
}
CarFactory factory = Car::new;
42. リストの中から偶数のみを
標準出力する(1)
• forEach
o [概要]: 個々の要素へ処理を加える
o [引数]: Block<? super T>
o [戻り値]: なし
o [Block の抽象メソッド]: void apply(T t)
43. リストの中から偶数のみを
標準出力する(2)
• filter
o [概要]: 条件にあった要素だけをフィルタリングする
o [引数]: Predicate<? super T>
o [戻り値]: Iterable<T>
o [Predicate の抽象メソッド]: boolean test(T t)
46. リストの中の身長の最大値を出力する
(1)
• map
o [概要]: 要素に処理を加えることで、新しいコレク
ションを生成する
o [引数]: Mapper<? super T,? extends U>
o [戻り値]: Iterable<U>
o [Mapper の抽象メソッド]: U map(T t)
47. リストの中の身長の最大値を出力する
(2)
• reduce
o [概要]: 要素をまとめて、1つの値を生成する
o [引数1]:結果の初期値
o [引数2]: BinaryOperator<T>
o [戻り値]: 最終的な結果
o [BinaryOperator の抽象メソッド]: T eval(T left,T
right)
48. リストの中の身長の最大値を出力する
(3)
class Student {
private final String name;
private final int height;
...
}
static final List<Student> STUDENT_LIST;
...
Iterable<Integer> heightList =
STUDENT_LIST.map(s -> s.getHeight());
int max = heightList.reduce(-1, (x, y) -> Math.max(x, y));
System.out.println(max);
49. リストの中の身長の最大値を出力する
(4)
class Student {
private final String name;
private final int height;
...
}
static final List<Student> STUDENT_LIST;
...
System.out.println(
STUDENT_LIST.map(s -> s.getHeight())
.reduce(-1, (x, y) -> Math.max(x, y)));