58. 58
関数型インターフェース(JLS9.8)
A functional interface is an interface that has just one abstract method, and thus represents a single function contract. (In some cases, this "single" method may take the form of multiple
abstract methods with override-equivalent signatures (8.4.2) inherited from superinterfaces; in this case, the inherited methods logically represent a single method.)
More precisely, for interface I, let M be the set of abstract methods that are members of I but that do not have the same signature as any public instance method of the class Object. Then I is
a functional interface if there exists a method m in M for which the following conditions hold:
The signature of m is a subsignature (8.4.2) of every method's signature in M.
m is return-type-substitutable (8.4.5) for every method in M.
In addition to the usual process of creating an interface instance by declaring (8.1) and instantiating (15.9) a class, instances of functional interfaces can be created with lambda expressions
(15.27), method references (15.28), or constructor references.
The function descriptor of a functional interface I is a method type (8.2) that can be used to legally override (8.4.8) the abstract method(s) of I.
Let M be the set of abstract methods defined above for I. The descriptor of I consists of the following:
Type parameters, formal parameters, and return types: Let m be a method in M with i) a signature that is a subsignature of every method's signature in M and ii) a return type that is a
subtype of every method's return type in M (after adapting for any type parameters (8.4.4)); if no such method exists, then let m be a method in M that i) has a signature that is a subsignature
of every method's signature in M and ii) is return-type-substitutable for every method in M. Then the descriptor's type parameters, formal parameter types, and return type are as given by m.
Thrown types: The descriptor's thrown types are derived from the throws clauses of the methods in M. If the descriptor is generic, these clauses are first adapted to the type parameters of
the descriptor (8.4.4); if the descriptor is not generic but at least one method in M is, these clauses are first erased. Then the descriptor's thrown types include every type, E, satisfying the
following constraints:
E is mentioned in one of the throws clauses.
For each throws clause, E is a subtype of some type named in that clause.
A functional interface type is one of the following:
A functional interface
A parameterization (4.5) of a functional interface
An intersection (4.9) of interface types that meets the following criteria:
Exactly one element of the intersection is a functional interface, or a parameterization of a functional interface. Let F be this interface.
A notional interface, I, that extends all the interfaces in the intersection would be a functional interface. If any of the intersection elements is a parameterized type, then I is generic: for
each element of the intersection that is a parameterized type J<A1...An>, let P1...Pn be the type parameters of J; then P1...Pn are also type parameters of I, and I extends the type
J<P1...Pn>.
The function descriptor of I is the same as the function descriptor of F.
The function descriptor of a parameterized functional interface, F<A1...An>, where A1...An are type arguments (4.5.1), is derived as follows. Let P1...Pn be the type parameters of F; types
T1...Tn are derived from the type arguments according to the following rules (for 1 ≤ i ≤ n):
If Ai is a type, then Ti = Ai.
If Ai is a upper-bounded wildcard ? extends Ui, then Ti = Ui.
If Ai is a lower-bounded wildcard ? super Li, then Ti = Li.
If Ai is an unbound wildcard ?, then if Pi has upper bound Bi that mentions none of P1...Pn, then Ti = Bi; otherwise, Ti = Object.
If F<T1...Tn> is a well-formed type, then the descriptor of F<A1...An> is the result of applying substitution [P1:=T1, ..., Pn:=Tn] to the descriptor of interface F. Otherwise, the descriptor of
F<A1...An> is undefined.
The function descriptor of an intersection that is a functional interface type is the same as the function descriptor of the functional interface or parameterization of a functional interface that is
an element of the intersection.
59. 59
関数型インターフェース(JLS9.8)
A functional interface is an interface that has just one abstract method, and thus represents a single function contract. (In some cases, this "single" method may take the form of multiple
abstract methods with override-equivalent signatures (8.4.2) inherited from superinterfaces; in this case, the inherited methods logically represent a single method.)
More precisely, for interface I, let M be the set of abstract methods that are members of I but that do not have the same signature as any public instance method of the class Object. Then I is
a functional interface if there exists a method m in M for which the following conditions hold:
The signature of m is a subsignature (8.4.2) of every method's signature in M.
m is return-type-substitutable (8.4.5) for every method in M.
In addition to the usual process of creating an interface instance by declaring (8.1) and instantiating (15.9) a class, instances of functional interfaces can be created with lambda expressions
(15.27), method references (15.28), or constructor references.
The function descriptor of a functional interface I is a method type (8.2) that can be used to legally override (8.4.8) the abstract method(s) of I.
Let M be the set of abstract methods defined above for I. The descriptor of I consists of the following:
Type parameters, formal parameters, and return types: Let m be a method in M with i) a signature that is a subsignature of every method's signature in M and ii) a return type that is a
subtype of every method's return type in M (after adapting for any type parameters (8.4.4)); if no such method exists, then let m be a method in M that i) has a signature that is a subsignature
of every method's signature in M and ii) is return-type-substitutable for every method in M. Then the descriptor's type parameters, formal parameter types, and return type are as given by m.
Thrown types: The descriptor's thrown types are derived from the throws clauses of the methods in M. If the descriptor is generic, these clauses are first adapted to the type parameters of
the descriptor (8.4.4); if the descriptor is not generic but at least one method in M is, these clauses are first erased. Then the descriptor's thrown types include every type, E, satisfying the
following constraints:
E is mentioned in one of the throws clauses.
For each throws clause, E is a subtype of some type named in that clause.
A functional interface type is one of the following:
A functional interface
A parameterization (4.5) of a functional interface
An intersection (4.9) of interface types that meets the following criteria:
Exactly one element of the intersection is a functional interface, or a parameterization of a functional interface. Let F be this interface.
A notional interface, I, that extends all the interfaces in the intersection would be a functional interface. If any of the intersection elements is a parameterized type, then I is generic: for
each element of the intersection that is a parameterized type J<A1...An>, let P1...Pn be the type parameters of J; then P1...Pn are also type parameters of I, and I extends the type
J<P1...Pn>.
The function descriptor of I is the same as the function descriptor of F.
The function descriptor of a parameterized functional interface, F<A1...An>, where A1...An are type arguments (4.5.1), is derived as follows. Let P1...Pn be the type parameters of F; types
T1...Tn are derived from the type arguments according to the following rules (for 1 ≤ i ≤ n):
If Ai is a type, then Ti = Ai.
If Ai is a upper-bounded wildcard ? extends Ui, then Ti = Ui.
If Ai is a lower-bounded wildcard ? super Li, then Ti = Li.
If Ai is an unbound wildcard ?, then if Pi has upper bound Bi that mentions none of P1...Pn, then Ti = Bi; otherwise, Ti = Object.
If F<T1...Tn> is a well-formed type, then the descriptor of F<A1...An> is the result of applying substitution [P1:=T1, ..., Pn:=Tn] to the descriptor of interface F. Otherwise, the descriptor of
F<A1...An> is undefined.
The function descriptor of an intersection that is a functional interface type is the same as the function descriptor of the functional interface or parameterization of a functional interface that is
an element of the intersection.
@
FunctionalInterface
62. 62
java.util.function.*
名前 第一引数 第二引数 戻り値
Consumer<T> T - void
Function<T, R> T - R
Predicate<T> T - boolean
Supplier<T> - - T
BiConsumer<T, U> T U void
BiFunction<T, U, R> T U R
BiPredicate<T, R> T U boolean
77. 77
ラムダ式のメソッド本体
(int n) -> {return n + 5;}
(n) -> {return n + 5;}
n -> {return n + 5;}
● return文のみ場合,return等を省略できる
(int n) -> n + 5
(n) -> n + 5
n -> n + 5
94. 94
ラムダ式の利用例
p -> p.getName()
s -> Integer.parseInt(s)
(k, v) -> map.put(k, v)
init -> new MyClass(init)
n -> new int[n]
● 引数を受け流すパターン
95. 95
ラムダ式の利用例
p -> p.getName()
s -> Integer.parseInt(s)
(k, v) -> map.put(k, v)
init -> new MyClass(init)
n -> new int[n]
● 引数を受け流すパターン
● メソッド・コンストラクタ参照
96. 96
メソッド・コンストラクタ参照
p -> p.getName()
s -> Integer.parseInt(s)
(k, v) -> map.put(k, v)
init -> new MyClass(init)
n -> new int[n]
● クラス名等::メソッド or new
Person::getName
Integer::perseInt
map::put
MyClass::new
int[]::new
109. 109
多重継承
class BasicPerson {
public boolean isMan() {/*...*/}
}
class ComplexPerson extends BasicPerson
implements Person {
public Sex getSex() {/*...*/}
}
113. 113
Class always win
class BasicPerson {
public boolean isMan() {/*...*/}
}
class ComplexPerson extends BasicPerson implements
Person {
public Sex getSex() {/*...*/}
}
● BasicPerson#isManが使われる
– “Class always win”
114. 114
親インターフェースの呼び出し
class ComplexPerson extends BasicPerson
implements Person {
public Sex getSex() {/*...*/}
public boolean isMan() {
return Person.super.isMan();
}
}
● インターフェース名.super.メソッド名(...)
127. 処理量の増加
for (Iterator<String> i = strings.iterator();
i.hasNext(); )
if (i.next().length > 10)
i.remove();
for (String s : strings)
System.out.println(s);
192. 192
年齢の合計,平均,最大,最小
int sum = 0, max = 0, min = 0;
double ave;
for (Person p : persons) {
int age = p.getAge();
sum += age;
max = Math.max(max, age);
min = Math.min(min, age);
}
ave = sum / (double)persons.size();
194. 194
年齢の合計,平均,最大,最小
int sum = persons.stream()
.mapToInt(Person::getAge).sum();
OptionalInt max = persons.stream()
.mapToInt(Person::getAge).max();
OptionalInt min = persons.stream()
.mapToInt(Person::getAge).min();
OptionalDouble ave = persons.stream()
.mapToInt(Person::getAge).average();
195. 195
年齢の合計,平均,最大,最小
int sum = persons.stream()
.mapToInt(Person::getAge).sum();
OptionalInt max = persons.stream()
.mapToInt(Person::getAge).max();
OptionalInt min = persons.stream()
.mapToInt(Person::getAge).min();
OptionalDouble ave = persons.stream()
.mapToInt(Person::getAge).average();
ループ四回
196. 196
年齢の合計,平均,最大,最小
IntStream s = persons.stream()
.mapToInt(Person::getAge);
int sum = s.sum();
OptionalInt max = s.max();
OptionalInt min = s.min();
OptionalDouble ave = s.average();
197. 197
年齢の合計,平均,最大,最小
IntStream s = persons.stream()
.mapToInt(Person::getAge);
int sum = s.sum();
OptionalInt max = s.max();
OptionalInt min = s).min();
OptionalDouble ave = s.average();
IllegalStateException
200. 200
年齢の合計,平均,最大,最小
IntSummaryStatistics iss = persons.stream()
.mapToInt(Person::getAge)
.summaryStatistics();
int sum = iss.getSum();
int min = iss.getMin(), max = iss.getMax();
double ave = iss.getAverage();
int count = iss.getCount();
227. 227
Parallel
collect(SB::new, SB::append, SB::append)
H e l l o , W o r l d !
Hell Wo SBd!SBo, SBrlWoWoWoWo
H e o ,l l W o r l d !
Hell SBd!SBd!SBllHeHeWod!Wo SBd!WoWoWoWo SBd!SBd!SBllHeHeWod!SBd!SBd!SBd!SBllHeHeWod!SBd!SBd!SBd!SBllHeHeWorlSBd!SBd!SBd!SBllHeHeWod!SBd!SBd!SBd!SBllHeHeWoo,
228. 228
Parallel
collect(SB::new, SB::append, SB::append)
H e l l o , W o r l d !
Hell Wo SBd!SBo, SBrlWoWoWoWo
H e o ,l l W o r l d !
Hell SBd!SBd!SBllHeHeWod!SBo, SBrlSBd!SBd!SBd!SBllHeHeWod!SBd!SBd!SBd!SBllHeHeWorlSBd!SBd!SBd!SBllHeHeWod!SBd!SBd!SBd!SBllHeHeWoo,
243. 243
社員の部署別のMapを得たい
Map<Dep, List<Emp>> empsByDep =
new HashMap<>();
for (Emp e : emps) {
Dep d = e.getDep();
if (empsByDep.containsKey(d)) {
empsByDep.put(d, new ArrayList<>());
}
empsByDep.get(d).add(e);
}