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

Support ifAbsent() method in java.util.Optional

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Currently `java.util.Optional` has ifPresent() method if we want to do something if the value is present.

      Whereas if we want to do something "only" when the value is absent (empty), we are forced to use Optional.ifPresentOrElse().

      In this case, we are forced to write an empty consumer / lambda (stale code) in ifPresent part like following
      Optional.ifPresentOrElse(unused -> {}, doSomethingWhenEmpty());

      Or use an if condition like following which is not so expressive
      if (Optional.isEmpty()) {
        doSomethingWhenEmpty()
      }

      Having, Optional.ifAbsent() would be handy in these scenarios.


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: