This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,539 Users
  • 2,269,776 Discussions
  • 7,916,824 Comments

Discussions

Spliterator.trySplit() ?

SASWATA MANDAL
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

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