【Java】ラムダ式 ~ 応用編 / サンプル集・その1 ~

■ はじめに

https://blogs.yahoo.co.jp/dk521123/34188394.html
https://blogs.yahoo.co.jp/dk521123/34192426.html
で行ったことを使って、よく使うであろうサンプル集を扱う

■ IDの昇順/降順に並び替える

sort() を使う
# 昇順(小さい順)
list.sort((x1, x2) -> (int) (x1.getId() - x2.getId()));

# 降順(大きい順)
list.sort((x1, x2) -> (int) (x2.getId() - x1.getId()));

■ 日時以降/以前のデータを取得する

filter() と before/equals/after を使う
// targetDatetime = 比較対象の日時
Timestamp targetDatetime = new Timestamp(new SimpleDateFormat("yyyy/MM/dd").parse("1989/03/05").getTime());
List<Person> results = list.stream().filter(x -> x.getBirthDate().after(targetDatetime)).collect(Collectors.toList());
before/equals/after については、以下の参考文献を参照のこと。
http://java-code.jp/215

サンプル

Person.java

import java.sql.Timestamp;

public class Person {
  private Long id;
  private String name;
  private String sex;
  private Timestamp birthDate;
  
  public Person(
        Long id, String name, String sex, Timestamp birthDate) {
     this.id = id;
     this.name = name;
     this.sex = sex;
     this.birthDate = birthDate;
  }
  
  public Long getId() {
     return id;
  }
  public void setId(Long id) {
     this.id = id;
  }
  public String getName() {
     return name;
  }
  public void setName(String name) {
     this.name = name;
  }
  public String getSex() {
     return sex;
  }
  public void setSex(String sex) {
     this.sex = sex;
  }
  public Timestamp getBirthDate() {
     return birthDate;
  }
  public void setBirthDate(Timestamp birthDate) {
     this.birthDate = birthDate;
  }
}

SampleLambda.java

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class SampleLambda {

  public static void main(String[] args) throws ParseException {
    List<Person> people = new ArrayList<Person>();
    Person person1 = new Person(1L, "Mike", "m",
        new Timestamp(new SimpleDateFormat("yyyy/MM/dd").parse("1989/03/05").getTime()));
    Person person2 = new Person(5L, "Tom", "m",
        new Timestamp(new SimpleDateFormat("yyyy/MM/dd").parse("1972/11/25").getTime()));
    Person person3 = new Person(2L, "Naomi", "f",
        new Timestamp(new SimpleDateFormat("yyyy/MM/dd").parse("2003/06/09").getTime()));
    Person person4 = new Person(4L, "Amy", "f",
        new Timestamp(new SimpleDateFormat("yyyy/MM/dd").parse("2001/09/12").getTime()));
    Person person5 = new Person(3L, "Ken", "m",
        new Timestamp(new SimpleDateFormat("yyyy/MM/dd").parse("2010/07/25").getTime()));
    people.add(person1);
    people.add(person2);
    people.add(person3);
    people.add(person4);
    people.add(person5);

    System.out.println("Ex1");
    List<Person> results = people.stream().filter(x -> x.getSex().equals("m")).collect(Collectors.toList());
    Optional<Timestamp> youngest = results.stream().map(x -> x.getBirthDate()).max((a, b) -> a.compareTo(b));
    System.out.println("Result : " + youngest.get().toString());

    System.out.println("Ex2");
    Timestamp targetDatetime = new Timestamp(new SimpleDateFormat("yyyy/MM/dd").parse("2002/03/05").getTime());
    results = people.stream().filter((x) -> x.getBirthDate().after(targetDatetime)).collect(Collectors.toList());
    for (Person result : results) {
      System.out
          .println("Result : " + result.getId() + " " + result.getName() + " " + result.getBirthDate().toString());
    }

    System.out.println("Ex3");
    results = people.stream().sorted((x1, x2) -> x2.getBirthDate().compareTo(x1.getBirthDate())).limit(3)
        .collect(Collectors.toList());
    for (Person result : results) {
      System.out.println("Result : " + result.getId() + " " + result.getName());
    }

    System.out.println("Ex4");
    people.sort((x1, x2) -> x1.getId().compareTo(x2.getId()));
    for (Person result : people) {
      System.out.println("Result : " + result.getId() + " " + result.getName());
    }

    System.out.println("Ex5");
    people.sort((x1, x2) -> (int) (x2.getId() - x1.getId()));
    for (Person result : people) {
      System.out.println("Result : " + result.getId() + " " + result.getName());
    }

    System.out.println("Ex6");
    people.removeIf(x -> x.getId() > 2);
    for (Person person : people) {
      System.out.println("Result : " + person.getId() + " " + person.getName());
    }
  }
}

出力結果

Ex1
Result : 2010-07-25 00:00:00.0
Ex2
Result : 2 Naomi 2003-06-09 00:00:00.0
Result : 3 Ken 2010-07-25 00:00:00.0
Ex3
Result : 3 Ken
Result : 2 Naomi
Result : 4 Amy
Ex4
Result : 1 Mike
Result : 2 Naomi
Result : 3 Ken
Result : 4 Amy
Result : 5 Tom
Ex5
Result : 5 Tom
Result : 4 Amy
Result : 3 Ken
Result : 2 Naomi
Result : 1 Mike
Ex6
Result : 2 Naomi
Result : 1 Mike

関連記事

Javaラムダ式 ~入門編~

https://blogs.yahoo.co.jp/dk521123/34188394.html

Javaラムダ式 ~ 基本編 / Stream API

https://blogs.yahoo.co.jp/dk521123/34192426.html

ラムダ式 ~ 応用編 / サンプル集・その1 ~

https://blogs.yahoo.co.jp/dk521123/37093852.html

ラムダ式 ~ 応用編 / サンプル集・その2 / 過去直近データを取得する ~

https://blogs.yahoo.co.jp/dk521123/37132834.html