Advertisement
DulcetAirman

for-else

Jul 8th, 2016
1,679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  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 :-("));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement