SHARE
TWEET

for-else

DulcetAirman Jul 8th, 2016 (edited) 1,530 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     boolean doElse = true;
  3.     for (final Foo foo : this.getFoos()) {
  4.         // process the foo
  5.         doElse = false;
  6.     }
  7.     if (doElse) {
  8.       // There was not a single foo
  9.     }  
  10. }
  11.  
  12. // ----------------------------------------
  13. // How it would be with for-else:
  14.  
  15.     for (final Foo foo : this.getFoos()) {
  16.       // process the foo
  17.     } else {
  18.       // There was not a single foo
  19.     }  
  20.  
  21. // -----------------------------------------
  22. // Java 8 Stream API:
  23.  
  24. getFoos().stream().forEachElse(f -> f.process(), () -> System.out.println("No foo :-("));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top