Example1. Simple example
button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View view){ //Do something } });
In the above example, what we really need is inside of onClick() method. Other code is actually decorations and we don't want it. How about we can pass just onClick method body as a parameter of setOnClickListener method like below?
button1.setOnClickListener(view->{ //Do something });
That's what we have exactly wanted. We can use the code style in Java 8. I think you already might know about Java 8 Lambda but there is no official support for Java 8 in Android 8. It would be awesome we can use it in Android and there is a way to be able to use Lambda in Android as well. You can refer my previous blog to setup RetroLambda in Android Studio.
button1.setOnClickListener(view->{ //Do something });
Example2. Creating an AlertDialog
public static Dialog showDialog(Context context) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // FIRE ZE MISSILES! } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it return builder.create(); }
It has too many boiler plate code to show just a dialog. Let's change the above code using Lambda expression.
public static Dialog showDialog(Context context) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, (dialog,id) -> { // FIRE ZE MISSILES! }) .setNegativeButton(R.string.cancel, (dialog,id) -> { // User cancelled the dialog }); // Create the AlertDialog object and return it return builder.create(); }
Example3. Creating an instance
You can use Lambda expression when you create an instance as well.
View.OnClickListener clickListener = (view) -> { };
However, you can use same parameter name like the below because Lambda try to use same name 'view' with the method:
private void createOnClickListener(View view) { View.OnClickListener clickListener = (view) -> { }; }
Example4. A tricky example
Here are two interfaces. Notice that Callable interface returns generic type T.
public interface Runnable{ public void run(); }
public interface Callable{ public T call(); }
Suppose that you methods using those interfaces:
public void invoke(Runnable r){ r.run(); } publicT invoke(Callable c){ return c.call(); }
If you call this statement, which method above will be executed?
String s = invoke(() -> "Completed");
In the Lambda expression, it returns a String "Completed". So, the method invoke(Callable
will be executed. It will be like below if I translated to non-Lamdba version.
String s = invoke(new Callable(){ @Override public String call(){ return "Completed"; } });
Comments
Post a Comment