Lambda Expression in Android Using Java 8

Lambda Expression in Android Using Java 8

What is Lambda Expressions?

Lambda Expressions are one of the most useful features in Java 8. Implementing interface using implement its abstract method is too long, but using Lambda Expressions we can pass functionality directly to an argument to the method. Here are some examples of that.

Lambda Expression help for Button click Listener.

Without using Lambda:

button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
       Toast.makeText(MainActivity.this, "Button Click", Toast.LENGTH_SHORT).show();
   }
});

 Using Lambda:

button.setOnClickListener(view -> Toast.makeText(MainActivity.this, "Button Click", Toast.LENGTH_SHORT).show());

 How to add Lambda Expressions in Android?

To use Lambda Expressions and other Java 8 features we need to update app build.gradle. You just add code in default Config section.

compileOptions {
   sourceCompatibility JavaVersion.VERSION_1_8

   targetCompatibility JavaVersion.VERSION_1_8

}

After that sync, your project and you can use Lambda Expressions.

How to use Lambda Expressions on ArrayList?

Before Without using Lambda Expression, if we want to create search facility using ArrayList, we have to use foreach and check multiple times, it leads us to a number of lines of code. Using Lambda Expression we can replace the number of line of code to a single line of code.

Without using Lambda:

ArrayList<StateCity> list = getStateCity();
String state = "";
for(StateCity model : list){
   if(model.getCity().equals(city)){
       state = model.getState();
       break;
   }
}
Toast.makeText(this, "Your State is : " + state, Toast.LENGTH_SHORT).show();

Using Lambda:

ArrayList<StateCity> list = getStateCity();
Optional<StateCity> model = list.stream().filter((StateCity)->StateCity.getCity().equals(city)).findFirst();
Toast.makeText(this, "Your State is : " + model.getState(), Toast.LENGTH_SHORT).show();

 Another example for searching employees whose salary is more than 20K without and with using Lamda.

Without using Lambda:

ArrayList<EmployeeModel> list = getEmployees();
ArrayList<EmployeeModel> result = new ArrayList<>();
for(EmployeeModel model : list){
   if(model.getSalary() > 20000){
       result.add(model); 
   }
}

Using Lambda:

ArrayList<EmployeeModel> list = getEmployees();
Collection<EmployeeModel> collect = list.stream().filter((EmployeeModel)->EmployeeModel.getSalary()>20000).collect(Collectors.toList());
ArrayList<EmployeeModel> result = new ArrayList<>(collect);

 How to use Lambda Expressions for Multi Thread

Multi Threading is another scenario where we can use Lambda Expression to write short and clean code.

Without using Lambda:

Runnable r = new Runnable(){
   @Override
   public void run(){
       System.out.println("Run new Thread");               
   }
};
Thread thread = new Thread(r);
thread.start();

 Using Lambda:

Runnable r = () -> System.out.println("Run new Thread");
new Thread(r).start();