Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Java lambda instead of 'if else'

With Java 8, I have this code:

if(element.exist()){     // Do something } 

I want to convert to lambda style,

element.ifExist(el -> {     // Do something }); 

with an ifExist method like this:

public void ifExist(Consumer<Element> consumer) {     if (exist()) {         consumer.accept(this);     } } 

But now I have else cases to call:

element.ifExist(el -> {     // Do something }).ifNotExist(el -> {     // Do something }); 

I can write a similar ifNotExist, and I want they are mutually exclusive (if the exist condition is true, there is no need to check ifNotExist, because sometimes, the exist() method takes so much workload to check), but I always have to check two times. How can I avoid that?

Maybe the "exist" word make someone misunderstand my idea. You can imagine that I also need some methods:

ifVisible() ifEmpty() ifHasAttribute() 

Many people said that this is bad idea, but:

In Java 8 we can use lambda forEach instead of a traditional for loop. In programming for and if are two basic flow controls. If we can use lambda for a for loop, why is using lambda for if bad idea?

for (Element element : list) {     element.doSomething(); }  list.forEach(Element::doSomething); 

In Java 8, there's Optional with ifPresent, similar to my idea of ifExist:

Optional<Elem> element = ... element.ifPresent(el -> System.out.println("Present " + el); 

And about code maintenance and readability, what do you think if I have the following code with many repeating simple if clauses?

if (e0.exist()) {     e0.actionA(); } else {     e0.actionB(); }  if (e1.exist()) {     e0.actionC(); }  if (e2.exist()) {     e2.actionD(); }  if (e3.exist()) {     e3.actionB(); } 

Compare to:

e0.ifExist(Element::actionA).ifNotExist(Element::actionB); e1.ifExist(Element::actionC); e2.ifExist(Element::actionD); e3.ifExist(Element::actionB); 

Which is better? And, oops, do you notice that in the traditional if clause code, there's a mistake in:

if (e1.exist()) {     e0.actionC(); // Actually e1 } 

I think if we use lambda, we can avoid this mistake!

like image 518
yelliver Avatar asked Apr 16 '18 12:04

yelliver


People also ask

Can we use if condition in lambda expression Java?

The 'if-else' condition can be applied as a lambda expression in forEach() function in form of a Consumer action.

How do you handle if-else in stream?

Conventional if/else Logic Within forEach() First of all, let's create an Integer List and then use conventional if/else logic within the Integer stream forEach() method: List<Integer> ints = Arrays. asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); ints. stream() .

Can we use lambda expression for non functional interface?

Lambda expressions can operate only on functional interfaces (i.e. interface with only one abstract method).


Video Answer


1 Answers

As it almost but not really matches Optional, maybe you might reconsider the logic:

Java 8 has a limited expressiveness:

Optional<Elem> element = ... element.ifPresent(el -> System.out.println("Present " + el); System.out.println(element.orElse(DEFAULT_ELEM)); 

Here the map might restrict the view on the element:

element.map(el -> el.mySpecialView()).ifPresent(System.out::println); 

Java 9:

element.ifPresentOrElse(el -> System.out.println("Present " + el,                         () -> System.out.println("Not present")); 

In general the two branches are asymmetric.

like image 124
Joop Eggen Avatar answered Oct 10 '22 17:10

Joop Eggen