Skip to content

Instantly share code, notes, and snippets.

@andrewrlee
Created January 24, 2015 16:38
Show Gist options
  • Save andrewrlee/97d2dcfd9fe2773ad8e7 to your computer and use it in GitHub Desktop.
Save andrewrlee/97d2dcfd9fe2773ad8e7 to your computer and use it in GitHub Desktop.
java 8 play
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Employee> list = new ArrayList<>();
list.add(new Employee(22, "bob"));
list.add(new Employee(20, "bob2"));
list.add(new Employee(42, "bob3"));
list.add(new Employee(32, "bob4"));
Predicate<Employee> a = Employee::isOld;
List<String> result = list.stream().filter(Employee::isOld)
.map(Employee::getName).collect(Collectors.toList());
// VS
List<String> names = new ArrayList<>();
for (Employee e : list) {
if (e.isOld()) {
names.add(e.getName());
}
}
}
public static class Employee {
private int age;
private String name;
public Employee(int age, String name) {
super();
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public boolean isOld() {
return age > 20;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment