I’m curious if there are things in the standard class library that you find useful but not widely used.

  • presumably_wrong
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Instead of wrapping it in an optional you can do Objects.requireNonNullElse(value, defaultValue)

    • austin@programming.dev
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      1 year ago

      Optional 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