回答編集履歴
3
追記
test
CHANGED
@@ -14,4 +14,22 @@
|
|
14
14
|
|
15
15
|
```
|
16
16
|
|
17
|
+
異常を伝えるためにもOptionalで返すべきですね。
|
18
|
+
|
19
|
+
```java
|
20
|
+
|
21
|
+
public Optional<Integer> getNum3(Integer num1,Integer num2){
|
22
|
+
|
23
|
+
Optional<Integer> i1 = Optional.ofNullable(num1),
|
24
|
+
|
25
|
+
i2 = Optional.ofNullable(num2);
|
26
|
+
|
27
|
+
return i1.flatMap(x -> i2.map(y -> y - x));
|
28
|
+
|
29
|
+
}
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
|
17
35
|
参考→[JavaのOptionalのモナド的な使い方 - 複数のOptional](http://qiita.com/koher/items/6f4a8d8b3ad3142bf645#%E8%A4%87%E6%95%B0%E3%81%AEoptional)
|
2
引き算の順番が逆だった
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
i2 = Optional.ofNullable(num2);
|
10
10
|
|
11
|
-
return i1.flatMap(x -> i2.map(y ->
|
11
|
+
return i1.flatMap(x -> i2.map(y -> y - x)).orElse(0);
|
12
12
|
|
13
13
|
}
|
14
14
|
|
1
返り値がOptionalのままだった
test
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
i2 = Optional.ofNullable(num2);
|
10
10
|
|
11
|
-
return i1.flatMap(x -> i2.map(y -> x - y));
|
11
|
+
return i1.flatMap(x -> i2.map(y -> x - y)).orElse(0);
|
12
12
|
|
13
13
|
}
|
14
14
|
|