Java Platform, Standard Edition 8 Early Access with Lambda Support b94 の optional クラスに filter map flatMap など愉快な仲間達が追加されたようです。
早速試してみました。
ネタ元はこちらです。
Java8でのプログラムの構造を変えるOptional、ただしモナドではない
この記事ではモナドについて触れられてますが私はモナドさっぱり解りません。
それって最中の親戚で甘くて美味しいの?ってレベルです。(^_^;)
それではまるっとコードを美味しくいただいて動くかどうか試してみます。
コードはいつものデータを利用します。
無駄に長いです。
<– Person –>
柴田 恭平, 61歳, Gender: MALE 体重 : 70 評価 : 8
壇 蜜, 32歳, Gender: FEMALE 体重 : 60 評価 : 6
北川 景子, 26歳, Gender: FEMALE 体重 : 55 評価 : 7
綾瀬 はるか, 28歳, Gender: FEMALE 体重 : 50 評価 : 4
佐々木 希, 25歳, Gender: FEMALE 体重 : 48 評価 : 9
剛力 彩芽, 20歳, Gender: FEMALE 体重 : 45 評価 : 6
小栗 旬, 30歳, Gender: MALE 体重 : 65 評価 : 3
堀北 真希, 24歳, Gender: FEMALE 体重 : 45 評価 : 10
武井 咲, 19歳, Gender: FEMALE 体重 : 50 評価 : 5
市原 隼人, 26歳, Gender: MALE 体重 : 67 評価 : 2
深田 恭子, 30歳, Gender: FEMALE 体重 : 50 評価 : 8
上記のリストで一番若い男性のもとへ一番若い女性がお嫁入りするとしてその女性の新しい姓名を表示させてみます。
ほげほげ@ちょめちょめ ってね。
package jp.yucchi.tryoptional;
import java.time.LocalDate;
import java.time.Period;
public class Person {
public enum Sex {
MALE, FEMALE
}
String firstName;
String lastName;
LocalDate birthday;
Sex gender;
int weight;
int evaluation;
public Person(String firstName, String lastName, LocalDate birthday, Sex gender, int weight, int evaluation) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = birthday;
this.gender = gender;
this.weight = weight;
this.evaluation = evaluation;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Sex getGender() {
return gender;
}
public int getAge() {
return Period.between(birthday, LocalDate.now()).getYears();
}
void printPerson() {
System.out.println(firstName + " " + lastName + ", " + this.getAge() + "歳" + ", Gender: "
+ this.getGender() + " 体重 : " + this.getWeight() + " 評価 : " + this.getEvaluation());
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getEvaluation() {
return evaluation;
}
public void setEvaluation(int evaluation) {
this.evaluation = evaluation;
}
}
package jp.yucchi.tryoptional;
import java.time.LocalDate;
import java.util.ArrayList;
import static java.util.Comparator.comparing;
import java.util.List;
import java.util.Optional;
import java.util.function.ToIntFunction;
public class TryOptional {
public static void main(String[] args) {
List person = new ArrayList<>();
person.add(new Person("柴田", "恭平", LocalDate.of(1951, 8, 18), Person.Sex.MALE, 70, 8));
person.add(new Person("壇", "蜜", LocalDate.of(1980, 12, 3), Person.Sex.FEMALE, 60, 6));
person.add(new Person("北川", "景子", LocalDate.of(1986, 8, 22), Person.Sex.FEMALE, 55, 7));
person.add(new Person("綾瀬", "はるか", LocalDate.of(1985, 3, 24), Person.Sex.FEMALE, 50, 4));
person.add(new Person("佐々木", "希", LocalDate.of(1988, 2, 8), Person.Sex.FEMALE, 48, 9));
person.add(new Person("剛力", "彩芽", LocalDate.of(1992, 8, 27), Person.Sex.FEMALE, 45, 6));
person.add(new Person("小栗", "旬", LocalDate.of(1982, 12, 26), Person.Sex.MALE, 65, 3));
person.add(new Person("堀北", "真希", LocalDate.of(1988, 10, 6), Person.Sex.FEMALE, 45, 10));
person.add(new Person("武井", "咲", LocalDate.of(1993, 12, 25), Person.Sex.FEMALE, 50, 5));
person.add(new Person("市原", "隼人", LocalDate.of(1987, 2, 6), Person.Sex.MALE, 67, 2));
person.add(new Person("深田", "恭子", LocalDate.of(1982, 11, 2), Person.Sex.FEMALE, 50, 8));
Optional byYoungestMan_firstName;
Optional byYoungestWoman_lastName;
ToIntFunction byAge = p -> p.getAge();
byYoungestMan_firstName = person.stream()
.filter(e -> e.getGender() == Person.Sex.MALE)
.sorted(comparing(byAge))
.map(Person::getFirstName)
.findFirst();
byYoungestWoman_lastName = person.stream()
.filter(e -> e.getGender() == Person.Sex.FEMALE)
.sorted(comparing(byAge))
.map(Person::getLastName)
.findFirst();
if (byYoungestMan_firstName.isPresent()) {
System.out.println(byYoungestMan_firstName.get());
}
if (byYoungestWoman_lastName.isPresent()) {
System.out.println(byYoungestWoman_lastName.get());
}
Optional fullName = byYoungestMan_firstName.flatMap(fn -> byYoungestWoman_lastName.map(ln -> String.join("@", fn, ln)));
if (fullName.isPresent()) {
System.out.println(fullName.get());
}
}
}
実行結果は以下のようになります。
市原
咲
市原@咲
期待通りに動いてるけどこれが可能になることが Java な人には重要なことなんだろうか?
モナドを美味しくいただけくことができない私にはイマイチ理解ができないです。
もしこれが重要なことだとしたらそのうち誰かが記事にして熱く語ってくれるだろう。