JackbyDev@programming.devM to Java@programming.dev · 1 year agoWhat are some things in the class library you wish more people knew about or used?docs.oracle.comexternal-linkmessage-square14fedilinkarrow-up13arrow-down10file-text
arrow-up13arrow-down1external-linkWhat are some things in the class library you wish more people knew about or used?docs.oracle.comJackbyDev@programming.devM to Java@programming.dev · 1 year agomessage-square14fedilinkfile-text
I’m curious if there are things in the standard class library that you find useful but not widely used.
minus-squarepresumably_wrong@lemm.eelinkfedilinkEnglisharrow-up1·1 year agoInstead of wrapping it in an optional you can do Objects.requireNonNullElse(value, defaultValue)
minus-squareaustin@programming.devlinkfedilinkEnglisharrow-up1·edit-21 year agoOptional has more syntactic sugar for more complex scenarios / functional call chaining that prevents repetitive if checks Optional.ofNullable(myObj) .map(MyClass::getProperty) .map(MyOtherClass::getAnotherProperty) .filter(obj -> somePredicate(obj)) .orElse(null) This is completely null safe, the function calls are only made if the object is not null
Instead of wrapping it in an optional you can do
Objects.requireNonNullElse(value, defaultValue)
Optional has more syntactic sugar for more complex scenarios / functional call chaining that prevents repetitive
if
checksOptional.ofNullable(myObj) .map(MyClass::getProperty) .map(MyOtherClass::getAnotherProperty) .filter(obj -> somePredicate(obj)) .orElse(null)
This is completely null safe, the function calls are only made if the object is not null