Spliterator.trySplit() ? — oracle-tech

    Forum Stats

  • 3,707,910 Users
  • 2,240,909 Discussions
  • 7,839,920 Comments

Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Spliterator.trySplit() ?

SASWATA MANDALSASWATA MANDAL 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 WegrzynowiczPatrycja Wegrzynowicz 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

Sign In or Register to comment.