Optional null pointer Terminator

9012 years, please give up (a! = Null && a.getFiled ()! = Null) or after the package is written (! ObejectUtil.isEmpty (a) &&! ObjectUtil.isEmpty (a.getFiled ())) the wording. Come under the Optional transfer it.

 

Optinal usage of a grasp of a large, not repeat them here, in order to prevent a null pointer, to keep in mind the following principles:

  • OfNullable rather than one of a, seen into the ofNullable source, this method can be accepted into the null reference, and the method of the participants to accept the null packet null pointer.

  • With orelse (orelseGet) rather than one get, the former can still avoid null pointer
  • When nested, the introduction map ,? e.g. Optinal.ofNullable (a) .map (Class :: getFieled) .map (Class :: getFieled2) .orElse (null); more elegant code.
  • Do non-empty judgment, can be introduced isParent
  • flatMap with Stream-based flatMap, not repeat them here.

Guess you like

Origin www.cnblogs.com/fbw-gxy/p/11595219.html