Lambdaの適用シーンインスタンス
8610 ワード
POJOクラスの定義
1つ目のケース:指定年齢を超えた人
2つ目のケース:一定の年齢範囲の人を選別する
package com.kotlin.demo.lambda;
import java.time.LocalDate;
public class Person {
public enum Sex {
MALE, FEMALE
}
String name;
int age;
LocalDate birthday;
Sex gender;
String emailAddress;
public Person(String name, int age, LocalDate birthday, Sex gender, String emailAddress) {
this.name = name;
this.age = age;
this.birthday = birthday;
this.gender = gender;
this.emailAddress = emailAddress;
}
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, Sex gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public int getAge() {
return this.age;
}
public String getName() {
return name;
}
public LocalDate getBirthday() {
return birthday;
}
public Sex getGender() {
return gender;
}
public String getEmailAddress() {
return emailAddress;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public void setGender(Sex gender) {
this.gender = gender;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public void printPerson() {
System.out.println("print person:" + this.toString());
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
", gender=" + gender +
", emailAddress='" + emailAddress + '\'' +
'}';
}
}
1つ目のケース:指定年齢を超えた人
public static void printPersonsOlderThan(List roster, int age) {
for (Person p : roster) {
if (p.getAge() >= age) {
p.printPerson();
}
}
}
List personList = new ArrayList<>();
personList.add(new Person("name1", 20, Person.Sex.MALE));
personList.add(new Person("name2", 30, Person.Sex.FEMALE));
personList.add(new Person("name3", 40, Person.Sex.MALE));
personList.add(new Person("name4", 50, Person.Sex.FEMALE));
personList.add(new Person("name5", 60, Person.Sex.MALE));
personList.add(new Person("name6", 70, Person.Sex.FEMALE));
personList.add(new Person("name7", 80, Person.Sex.MALE));
//
PersonUtil.printPersonsOlderThan(personList, 30);
2つ目のケース:一定の年齢範囲の人を選別する
public static void printPersonsWithinAgeRange(List roster, int low, int high) {
for (Person p : roster) {
if (low <= p.getAge() && p.getAge()
List personList = new ArrayList<>();
personList.add(new Person("name1", 20, Person.Sex.MALE));
personList.add(new Person("name2", 30, Person.Sex.FEMALE));
personList.add(new Person("name3", 40, Person.Sex.MALE));
personList.add(new Person("name4", 50, Person.Sex.FEMALE));
personList.add(new Person("name5", 60, Person.Sex.MALE));
personList.add(new Person("name6", 70, Person.Sex.FEMALE));
personList.add(new Person("name7", 80, Person.Sex.MALE));
PersonUtil.printPersonsWithinAgeRange(personList, 40, 60);
3つ のケース:フィルタ が なのか からないが、 の に するために.フィルタ クラスの public interface CheckPerson {
boolean test(Person p);
}
public class CheckPersonEligibleForSelectiveService implements CheckPerson {
@Override
public boolean test(Person p) {
return p.gender == Person.Sex.MALE &&
p.getAge() >= 20 &&
p.getAge() <= 40;
}
}
public static void printPersons(List roster, CheckPerson tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
List personList = new ArrayList<>();
personList.add(new Person("name1", 20, Person.Sex.MALE));
personList.add(new Person("name2", 30, Person.Sex.FEMALE));
personList.add(new Person("name3", 40, Person.Sex.MALE));
personList.add(new Person("name4", 50, Person.Sex.FEMALE));
personList.add(new Person("name5", 60, Person.Sex.MALE));
personList.add(new Person("name6", 70, Person.Sex.FEMALE));
personList.add(new Person("name7", 80, Person.Sex.MALE));
// CheckPerson person
PersonUtil.printPersons(personList, new CheckPersonEligibleForSelectiveService());
4の : の クラスによって されたフィルタインタフェースを し、 に じてList personList = new ArrayList<>();
personList.add(new Person("name1", 20, Person.Sex.MALE));
personList.add(new Person("name2", 30, Person.Sex.FEMALE));
personList.add(new Person("name3", 40, Person.Sex.MALE));
personList.add(new Person("name4", 50, Person.Sex.FEMALE));
personList.add(new Person("name5", 60, Person.Sex.MALE));
personList.add(new Person("name6", 70, Person.Sex.FEMALE));
personList.add(new Person("name7", 80, Person.Sex.MALE));
//
PersonUtil.printPersons(personList, new CheckPerson() {
@Override
public boolean test(Person p) {
return p.getGender() == Person.Sex.FEMALE
&& p.getAge() >= 40
&& p.getAge() <= 80;
}
});
5つ の : の クラスの わりにlambdaを するList personList = new ArrayList<>();
personList.add(new Person("name1", 20, Person.Sex.MALE));
personList.add(new Person("name2", 30, Person.Sex.FEMALE));
personList.add(new Person("name3", 40, Person.Sex.MALE));
personList.add(new Person("name4", 50, Person.Sex.FEMALE));
personList.add(new Person("name5", 60, Person.Sex.MALE));
personList.add(new Person("name6", 70, Person.Sex.FEMALE));
personList.add(new Person("name7", 80, Person.Sex.MALE));
// lambda
PersonUtil.printPersons(personList,
p -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 40
&& p.getAge() <= 80);
6つ の :フィルタ クラスを で するのではなく、JDKが したFuncationを するpublic static void printPersonsWithPredicate(List roster, Predicate tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
List personList = new ArrayList<>();
personList.add(new Person("name1", 20, Person.Sex.MALE));
personList.add(new Person("name2", 30, Person.Sex.FEMALE));
personList.add(new Person("name3", 40, Person.Sex.MALE));
personList.add(new Person("name4", 50, Person.Sex.FEMALE));
personList.add(new Person("name5", 60, Person.Sex.MALE));
personList.add(new Person("name6", 70, Person.Sex.FEMALE));
personList.add(new Person("name7", 80, Person.Sex.MALE));
PersonUtil.printPersonsWithPredicate(personList,
p -> p.getGender() == Person.Sex.MALE
&& p.getAge() >= 40
&& p.getAge() <= 80);
Stream でより くの を // lambda stream
personList.stream()
.filter(person -> person.getGender() == Person.Sex.MALE)
.forEach(person -> person.setEmailAddress("[email protected]"));
String s = personList.stream()
.filter(person -> person.getGender() == Person.Sex.MALE)
.map(person -> person.getName())
.findFirst()
.get();
まとめ: の を して、コードの のために、 、いくつかの クラスを して、 たちが んでいる を することがあります. クラスにより な が られるが,その なコード は,コード さを させる.lambdaの を し、コードをより します. に、lambdaは なコードロジックを するだけで、これらのコードをどのように するかはコンパイラに せて し、これらの の な を す はありません.また、コンパイラの に い、より い メカニズムがある があります.これにより、コードの な に せずに のみを できます.
:https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html