DulcetAirman icon

for-else

DulcetAirman | PRO | 07/08/16 12:33:06 PM UTC | 0 ⭐ | 2206 👁️ | Never ⏰ | []
Java |

520 B

|

None

|

0 👍

/

0 👎

{
    boolean doElse = true;
    for (final Foo foo : this.getFoos()) {
        // process the foo
        doElse = false;
    }
    if (doElse) {
      // There was not a single foo 
    }   
}
 
// ----------------------------------------
// How it would be with for-else:
 
    for (final Foo foo : this.getFoos()) {
      // process the foo
    } else {
      // There was not a single foo 
    }   
 
// -----------------------------------------
// Java 8 Stream API:
 
getFoos().stream().forEachElse(f -> f.process(), () -> System.out.println("No foo :-("));

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎