Created
December 9, 2012 02:22
-
-
Save tomoTaka01/4243014 to your computer and use it in GitHub Desktop.
Lambdaの実装を練習
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class PersonFind { | |
public static void main(String[] args) { | |
List<Person> persons = new ArrayList<>(); | |
persons.add(new Person("name10", 10)); | |
persons.add(new Person("name20", 20)); | |
persons.add(new Person("name30", 30)); | |
persons.add(new Person("name40", 40)); | |
persons.add(new Person("name50", 50)); | |
System.out.println("-- 1 filter ---------------------------"); | |
persons.stream().filter(p->p.getAge() >= 30).forEach(p -> System.out.println("★1 30以上:" + p.getName())); | |
System.out.println("-- 2 findFirst ---------------------------"); | |
// Optional<Person> firstPerson = | |
Object xxx = persons.stream().filter(p->p.getAge() == 20).findFirst(); | |
// Optional<Person> xxx = persons.stream().filter(p->p.getAge() == 20).findFirst(); | |
System.out.println("★2 firstPerson:" + xxx); | |
// System.out.println((Optional)xxx.get.getName()); | |
System.out.println("-- 3 map & sum ---------------------------"); | |
// int total = persons.stream().map(p->p.getAge()).sum(); | |
// System.out.println("total:" + total); | |
Object m = persons.stream().map(p->p.getAge()); | |
System.out.println("☆3 map:" + m); | |
System.out.println("-- 4 map & forEach ---------------------------"); | |
persons.stream().filter(p->p.getAge() < 20).map(p->p.getAge()).forEach(p->System.out.println("★4 map forEach:" + p)); | |
System.out.println("-- 5 map & into ---------------------------"); | |
List<Person> list = persons.stream().filter(p->p.getAge() <= 20).into(new ArrayList<Person>()); | |
list.forEach(p-> System.out.println("★5 map Into:" + p.getName())); | |
} | |
} | |
public class Person { | |
String name; | |
int age; | |
public Person(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getAge() { | |
System.out.println("getAge:" + age); | |
return age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment