Do you use in the development android application lambda? if so, what pitfalls are there? and is it worth using them?
Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants ߊߚߤߘ , Kromster , Sasha Omelchenko , αλεχολυτ , Mikhail Vaysman 17 Apr '17 at 0:08 .
The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .
1 answer
We use
There are no underwater stones. You can perceive them as syntactic sugar.
Worth it. It greatly reduces and facilitates the reading of the code.
An example of hanging a listener on a View on which one method should be called:
Without lambda
myView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { method(); } } With lambdas:
myView.setOnClickListener(view -> method()); Or so, if a method in the same class is defined and it accepts the input to the lambda:
myView.setOnClickListener(this::method); Total -5 lines of code and minus many characters only for this particular case. And there are thousands of them. Especially if some RxJava take
- oneI would add that a method in a class must accept the input to the lambda to use the call without explicitly passing the variable (this :: method) - zTrap
- @zTrap, added the answer) - YuriySPb ♦