Skip to content

Instantly share code, notes, and snippets.

@mathieuancelin
Created November 24, 2011 13:54
Show Gist options
  • Save mathieuancelin/1391398 to your computer and use it in GitHub Desktop.
Save mathieuancelin/1391398 to your computer and use it in GitHub Desktop.
class student {
String anme
int gradYear
double score
}
List<Student> students = ...;
double max = Double.MIN_VALUE;
for (Student s : students) {
if (s.gradYear == 2011) {
max = Math.max(max, s.score);
}
}
return max
double max =
students.filter(new Predicate<Student>() {
public boolean eval(Student s) {
return s.gradYear == 2011;
}
}).map(new Mapper<Student, Double>() {
public Double map(Student s) {
return s.score;
}
}).reduce(0.0, new Reducer<Double, Double>() {
public Double reduce(Double max, Double score) {
return Math.max(max, score);
}
});
double max =
students.filter((Student s) -> s.gradYear == 2011)
.map((Student s) -> s.score)
.reduce(0.0, (Double max, Double score) -> Math.max(max, score));
double max =
students.filter(s -> s.gradYear == 2011)
.map(s -> s.score)
.reduce(0.0, (max, score) -> Math.max(max, score));
double max =
students.filter(s -> s.gradYear == 2011)
.map(s -> s.score)
.reduce(0.0, Math#max); // method literal
double max =
students.parallel().filter(s -> s.gradYear == 2011) // iterable
.map(s -> s.score) // iterable
.reduce(0.0, Math#max); // double // method literal
interface Iterable<T> {
Iterator<T> iterator();
Iterable<T> filter(Predicate<? super T> predicate);
default Iterables.filter;
<U> Iterable<U> map(Mapper<? super T, ? extends U> mapper);
default Iterables.map;
<U> U reduce(U base, Reducer<U, ? super T> reducer);
default Iterables.reduce;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment