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
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:
Collections.sort(games, (a, b) ->;

Without Retrolambda:
Collections.sort(games, new Comparator<Game>() {
   public int compare(Game a, Game b) {

Java 8 lambdas also allow you to bind methods into lambdas (as long as the method signature matches the interface):

handler.postDelayed(this::hideToolbar, 1000);


handler.postDelayed(new Runnable() {
   public void run() {
}, 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.

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)

Powered By MyBB, © 2002-2024 Melroy van den Berg.