Forum Stats

  • 3,757,939 Users
  • 2,251,294 Discussions
  • 7,869,972 Comments

Discussions

Spliterator.trySplit() ?

SASWATA MANDAL
SASWATA MANDAL Member Posts: 4 Green Ribbon

Hi Experts ,

The trySplit() method of java.util.Spliterator<T> can return null or Spliterator<T>. So, since it can return null , its return type should be Optional<Spliterator<T>> . Optional<T> is introduced in JAVA 8 to handle NullPointerExceptions. So , why Optional is not used as its return type ?

Thanks & Regards

Saswata Mandal

Answers

  • Patrycja Wegrzynowicz
    Patrycja Wegrzynowicz Member Posts: 4 Red Ribbon

    The main reasons behind such a design decision:

    • trySplit is used internally in the JDK library (so no urgent need to expose a user-friendly API)
    • performance reasons to avoid an overhead of boxing/unboxing Optional (it's part of setting up a parallel pipeline, so to make it efficient it's better to avoid unnecessary performance overhead).

    Stuart Marks (Java architect) has described those two reasons in details in his post at: https://stackoverflow.com/a/30252175