05-17-2016, 08:55 PM
What is it? How does it work?
In Java 8, Oracle introduced lambdas. These lambdas are implemented as nameless anonymous classes, a feature that has been in Java for ages. Retrolambda is a compile time library that utilizes JDK 8 to convert classes that use lambdas to use normal anonymous classes.
Why should we use it?
Consider this example copied from port/android-agera:
With Retrolambda:
Without Retrolambda:
Java 8 lambdas also allow you to bind methods into lambdas (as long as the method signature matches the interface):
vs.
Compile time costs:
None, the process of converting Java 8 to Java 7 is extremely fast.
Who uses it?
According to GitHub, Retrolambda is used in 4,160 projects (judging from the number of Gradle files that include it). It could be used in even more places, since this is a compile time dependency.
In Java 8, Oracle introduced lambdas. These lambdas are implemented as nameless anonymous classes, a feature that has been in Java for ages. Retrolambda is a compile time library that utilizes JDK 8 to convert classes that use lambdas to use normal anonymous classes.
Why should we use it?
Consider this example copied from port/android-agera:
With Retrolambda:
Code:
Collections.sort(games, (a, b) -> a.name.compareTo(b.name));
Without Retrolambda:
Code:
Collections.sort(games, new Comparator<Game>() {
@Override
public int compare(Game a, Game b) {
return a.name.compareTo(b.name);
}
});
Java 8 lambdas also allow you to bind methods into lambdas (as long as the method signature matches the interface):
Code:
handler.postDelayed(this::hideToolbar, 1000);
vs.
Code:
handler.postDelayed(new Runnable() {
@Override
public void run() {
hideToolbar();
}
}, 1000);
Compile time costs:
None, the process of converting Java 8 to Java 7 is extremely fast.
Who uses it?
According to GitHub, Retrolambda is used in 4,160 projects (judging from the number of Gradle files that include it). It could be used in even more places, since this is a compile time dependency.