Java - Lambda expressions Jumps and Exits

Introduction

You can use break, continue, return, and throw inside the body of a lambda expression.

They jumps and exits inside the lambda expressions.

Non-local jumps and exits in lambda expressions are not allowed.

The following code demonstrates the valid use of the break and continue statements inside the body of a lambda expressions.

Demo

import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    Consumer<int[]> printer = ids -> {
      int printedCount = 0;
      for (int id : ids) {
        if (id % 2 != 0) {
          continue;
        }/*from   www  .j  av a  2  s  .  c  o  m*/
        if (printedCount == 3) {
          break;
        }
        System.out.println(id);
        printedCount++;

      }
    };

    printer.accept(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });
  }
}

Result

Quiz