Last active
February 10, 2018 03:19
-
-
Save jesperdj/8313548 to your computer and use it in GitHub Desktop.
Examples with Java 8 lambda expressions.
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 org.jesperdj.lambda; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Example01 { | |
// Using a for-loop (external iteration) | |
public Iterable<String> getBestStudentsOfYear(Iterable<Student> students, int year) { | |
List<String> result = new ArrayList<>(); | |
for (Student s : students) { | |
if (s.getGraduationYear() == year && s.getScore() >= 9) { | |
result.add(s.getName()); | |
} | |
} | |
return result; | |
} | |
} |
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 org.jesperdj.lambda; | |
import com.google.common.base.Function; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.Iterables; | |
public class Example02 { | |
// Using Google Guava with anonymous inner classes | |
public Iterable<String> getBestStudentsOfYear(Iterable<Student> students, int year) { | |
Iterable<Student> bestStudents = Iterables.filter(students, new Predicate<Student>() { | |
@Override | |
public boolean apply(Student s) { | |
return s.getGraduationYear() == year && s.getScore() >= 9; | |
} | |
}); | |
Iterable<String> names = Iterables.transform(bestStudents, new Function<Student, String>() { | |
@Override | |
public String apply(Student s) { | |
return s.getName(); | |
} | |
}); | |
return names; | |
} | |
} |
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 org.jesperdj.lambda; | |
import com.google.common.collect.Iterables; | |
public class Example03 { | |
// Using Google Guava with lambda expressions | |
public Iterable<String> getBestStudentsOfYear(Iterable<Student> students, int year) { | |
Iterable<Student> bestStudents = Iterables.filter(students, | |
(Student s) -> s.getGraduationYear() == year && s.getScore() >= 9); | |
Iterable<String> names = Iterables.transform(bestStudents, s -> s.getName()); | |
return names; | |
} | |
} |
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 org.jesperdj.lambda; | |
import com.google.common.collect.Iterables; | |
public class Example04 { | |
// Using Google Guava with lambda expressions and a method reference | |
public Iterable<String> getBestStudentsOfYear(Iterable<Student> students, int year) { | |
Iterable<Student> bestStudents = Iterables.filter(students, | |
s -> s.getGraduationYear() == year && s.getScore() >= 9); | |
Iterable<String> names = Iterables.transform(bestStudents, Student::getName); | |
return names; | |
} | |
} |
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 org.jesperdj.lambda; | |
public class Student { | |
private final String name; | |
private final int score; | |
private final int graduationYear; | |
public Student(String name, int score, int graduationYear) { | |
this.name = name; | |
this.score = score; | |
this.graduationYear = graduationYear; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getScore() { | |
return score; | |
} | |
public int getGraduationYear() { | |
return graduationYear; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment