Java中option&optional

Xiao Qiang Lv4

How to process nullable value in elegant way

optional in java

  • create optional
    • of, set a null will throw nullPointerException
    • ofNullable, set a null will throw no suchElementException
  • orElse will create a default value whatever this.isPresent is true or not
  • getOrElse the opposite of “orElse” operation
  • handle exception
    • orElseThrow works for optional.ofNullable
  • filter
    • This is used to check logic with different judge condition. incompatible for list operation
    • If list, we can use stream filter directly
  • transforming
    • map
      • convert a optional value to other value
      • also can filter a new list from original list with java stream operation
    • flatMap
      • for nested optional object, no need to unwrap, which can get it directly.
  • serialize
    • with Jackson to treat an empty Optional as null, and to treat a present Optional as a field representing its value.

option in vavr

  • option is used to eliminate nullable check, which can make our code more safe
  • you can add the following dependency for using option vavr
    1
    2
    3
    4
    5
    <dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr</artifactId>
    <version>0.9.0</version>
    </dependency>
  • create option
    • Option.of, then we can use getOrElse to get actual value or set a default value for returning
  • Tuple
  • Try
    • replace try-catch block
    • if it is failure, can use getOrElse
    • if dev wants to throw exception, can use getOrElseThrow
  • function Interface
    • Function
    • BiFunction
    • Function[num]
  • collections
    • include list, array, set, map, etc.
  • validation
    • combine
    • valid
    • invalid
    • ap
  • Match Pattern
    1
    2
    3
    Match(input).of(
    Case(...
    ))

source code

  • Title: Java中option&optional
  • Author: Xiao Qiang
  • Created at : 2023-03-05 14:11:21
  • Updated at : 2025-03-08 10:49:30
  • Link: http://fdslk.github.io/tech/java/java-feature-option-optional/2023/03/05/option-optional-difference/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments