I used Java 8 to write some logic like the following:
/** if(xxxOrder! = null){ if(xxxOrder.getXxxShippingInfo() ! = null){ if(xxxOrder.getXxxShippingInfo().getXxxShipmentDetails() ! = null){ if(xxxOrder.getXxxShippingInfo().getXxxShipmentDetails().getXxxTrackingInfo() ! = null){ ... }}}}Copy the code
I’m using Java 8’s optional. map method to eliminate layers of ifs.
There is one parameter, if the value is empty, take the value of several alternate parameters, if several alternate parameters are empty, take the default value.
For example, set the value of info. If info is empty, set info1, InfO2, Info3, and info4 in sequence. If info is empty, set default values.
It feels like the exact opposite of what we did last time, where we took the last value, and we took the first value that wasn’t null.
Implementation Scheme 1
Use if/else to implement:
/** source public id: Java technology stack */String info = xx.getInfo(); if (info == null) { info = xx.getInfo1(); if (info == null) { info = xx.getInfo2(); if (info == null) { info = xx.getInfo3(); if (info == null) { info = xx.getInfo4(); If (info == null) {info = "Java technology stack (default) "; }}}}}Copy the code
I bet there are people who would write this, and it’s not elegant to write this, but if you have a lot of alternate values, it’s not crazy.
Implementation Scheme 2
And, of course, there’s this:
/** source public id: Java technology stack */String info = null; if (xx.getInfo() ! = null) { info = xx.getInfo(); } else if (xx.getInfo1() ! = null) { info = xx.getInfo1(); } else if (xx.getInfo2() ! = null) { info = xx.getInfo2(); } else if (xx.getInfo3() ! = null) { info = xx.getInfo3(); } else if (xx.getInfo4() ! = null) { info = xx.getInfo4(); } else {info = "Java technology stack "; }Copy the code
This is a better way to write it than the first one, and it’s a more common way to write it.
Implementation Scheme 3 (recommended)
There’s another layer of if and else if, which is not very elegant, and it’s not recommended in many books, but it’s recommended:
Public String getInfo(XX XX) if (xx.getInfo()! = null) { return xx.getInfo(); } if (xx.getInfo1() ! = null) { return xx.getInfo1(); } if (xx.getInfo2() ! = null) { return xx.getInfo2(); } if (xx.getInfo3() ! = null) { return xx.getInfo3(); } if (xx.getInfo4() ! = null) { return xx.getInfo4; } return "Java technology stack "; }Copy the code
It’s a little smarter, and recommended, to extract a single method and return it one at a time to avoid too much if/else and potentially confusing logic.
Implementation Plan 4 (Final recommendation)
Even so, the code is still not very pretty. There are too many if judgments. Let’s see how Java 8 can kill if/else.
Examples of Java 8 scenarios:
/ ArrayList< String> List = new ArrayList<>(array.asList (info, Info1, infO2, Info3, info4, "Java stack (default) "); list.removeIf(StringUtils::isBlank); String result = list.get(0);Copy the code
3 lines done, cool!!
How does that happen? Let’s break it down line by line:
-
Build a List with each argument in turn
-
Remove all parameters that are null
-
Find the first parameter that is not null
Isn’t it elegant to use Java 8’s new removeIf method to subtly eliminate if/else and use Lambda expressions or method references?
Stream cannot accept a null argument, otherwise a pointer exception will be reported.