Skip to content

Instantly share code, notes, and snippets.

@tomoTaka01
Created December 9, 2012 02:48
Show Gist options
  • Save tomoTaka01/4243098 to your computer and use it in GitHub Desktop.
Save tomoTaka01/4243098 to your computer and use it in GitHub Desktop.
Lambda practice(do not know what is the problem)
import java.util.ArrayList;
import java.util.List;
public class PersonFindErr {
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> xxx = persons.stream().filter(p->p.getAge() == 20).findFirst();
System.out.println("★2 firstPerson:" + xxx);
System.out.println("-- 3 map & sum ---------------------------");
int total = persons.stream().map(p->p.getAge()).sum();
System.out.println("total:" + total);
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<>());
list.forEach(p-> System.out.println("★5 map Into:" + p.getName()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment