java - Is there a shortcut to execute something only if its not null? -
this question has answer here:
- avoiding != null statements 54 answers
i find myself writing statement
myobject myobject = something.getthatobject(); if( myobject !=null && myobject .somebooleanfunction()){ }
in order prevent null pointer exception. there shortcut in java? i'm thinking myobject..somebooleanfunction()
?
in java 8:
static <t> boolean notnull(supplier<t> getter, predicate<t> tester) { t x = getter.get(); return x != null && tester.test(x); } if (notnull(something::getthatobject, myobject::somebooleanfunction)) { ... }
if style new readers, 1 should keep in mind, full functional programming bit nicer.
Comments
Post a Comment