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

Stream APIの主なメソッド

filter

 * 条件に合致した要素だけ抜き出す

map

 * 要素全てに同じ処理を行い、その結果で新たにリストを作る

min / max

 * 最小 / 最大に合致した要素だけ抜き出す

sorted

 * ソートする

サンプル

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;
   }
}

SampleLambdaForStreamAPI.java

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

public class SampleLambdaForStreamAPI {
   public static void main(String[] args) throws ParseException {
      List<Person> people = new ArrayList<Person>();
      Person person1 = new Person(1, "Mike", "m", new Timestamp(
            new SimpleDateFormat("yyyy/MM/dd").parse("1989/03/05").getTime()));
      Person person2 = new Person(2, "Tom", "m", new Timestamp(
            new SimpleDateFormat("yyyy/MM/dd").parse("1972/11/25").getTime()));
      Person person3 = new Person(3, "Naomi", "f", new Timestamp(
            new SimpleDateFormat("yyyy/MM/dd").parse("2003/06/09").getTime()));
      Person person4 = new Person(4, "Amy", "f", new Timestamp(
            new SimpleDateFormat("yyyy/MM/dd").parse("2010/09/12").getTime()));
      Person person5 = new Person(5, "Ken", "m", new Timestamp(
            new SimpleDateFormat("yyyy/MM/dd").parse("2006/07/25").getTime()));
      people.add(person1);
      people.add(person2);
      people.add(person3);
      people.add(person4);
      people.add(person5);
      
      System.out.println("Ex1 Filter");
      List<Person> peopleForFilter = people.stream().filter(x -> x.getId() == 3).collect(Collectors.toList());
      for (Person person : peopleForFilter) {
         System.out.println("Result : " + person.getId() + " "
               + person.getName());
      }
      
      System.out.println("Ex2 Map");
      List<Long> peopleForMap = people.stream().map(x -> x.getId()).collect(Collectors.toList());
      for (Long id : peopleForMap) {
         System.out.println("Result : " + id);
      }
      
      System.out.println("Ex3 Max");
      Person person = people.stream().max((Person p1, Person p2) -> p1.getBirthDate().compareTo(p2.getBirthDate())).get();
      System.out.println("Result : " + person.getId() + " "
            + person.getName());
   }
}

出力結果

Ex1 Filter
Result : 3 Naomi
Ex2 Map
Result : 1
Result : 2
Result : 3
Result : 4
Result : 5
Ex3 Max
Result : 4 Amy


関連記事

ラムダ式 ~入門編~

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

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

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

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

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