Forum Stats

  • 3,853,698 Users
  • 2,264,256 Discussions


Spliterator.trySplit() ?

SASWATA MANDAL Member Posts: 5 Red 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


  • 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: