Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8236491

Method chain replacement for Optional.ifPresentOrElse()

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 14
    • core-libs

      A DESCRIPTION OF THE PROBLEM :
      Optional's new ifPresentOrElse() method is an improvement over the previous if/else idioms used for Optional, but it is somewhat clunky to declare your "ifPresent" logic after the language "orElse". E.g.,

      optional.ifPresentOrElse(System.out::println, System.out::println);

      If, instead, you could chain off the result of ifPresent(), it would result in much more fluid code:

      optional.ifPresent(System.out::println).orElse(System.out::println);

      This could be accomplished by making ifPresent() return itself and adding an orElse() method to optional, or by adding a new return type to ifPresent() specifically for chaining.


            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: