Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Android] Retrolambda: What it is, how it works, and why should we use it
#1
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:
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.
Reply


Messages In This Thread
[Android] Retrolambda: What it is, how it works, and why should we use it - by ds84182 - 05-17-2016, 08:55 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)