title: terminate or break java 8 stream loop
body: <p>I have a java 8 stream loop with the following content:</p>
  
  <pre><code>    void matchSellOrder(Market market, Order sellOrder) {
          System.out.println("selling " + market.pair() + " : " + sellOrder);
  
          market.buyOrders()
                  .stream()
                  .filter(buyOrder -&gt; buyOrder.price &gt;= sellOrder.price)
                  .sorted(BY_ASCENDING_PRICE)
                  .forEach((buyOrder) -&gt; {
                      double tradeVolume = Math.min(buyOrder.quantity, sellOrder.quantity);
                      double price = buyOrder.price;
  
                      buyOrder.quantity -= tradeVolume;
                      sellOrder.quantity -= tradeVolume;
  
                      Trade trade = new Trade.Builder(market, price, tradeVolume, Trade.Type.SELL).build();
                      CommonUtil.convertToJSON(trade);
  
                      if (sellOrder.quantity == 0) {
                          System.out.println("order fulfilled");
                          // break loop there
                      }
                  });
      }
  </code></pre>
  
  <p>How can I break out of loop when some condition is met?
  Whats the right way to close stream anyway?</p>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>I was misusing streams tecnique assuming that it is a loop, it is not designed for that. Here's the code I've ended up using answer provided below:</p>
  
  <pre><code>        List&lt;Order&gt; applicableSortedBuyOrders = market.buyOrders()
                  .stream()
                  .filter(buyOrder -&gt; buyOrder.price &gt;= sellOrder.price)
                  .sorted(BY_ASCENDING_PRICE)
                  .collect(toList());
  
          for(Order buyOrder : applicableSortedBuyOrders){
              double tradeVolume = Math.min(buyOrder.quantity, sellOrder.quantity);
              double price = buyOrder.price;
  
              buyOrder.quantity -= tradeVolume;
              sellOrder.quantity -= tradeVolume;
  
              Trade trade = new Trade.Builder(market, price, tradeVolume, Trade.Type.SELL).build();
              CommonUtil.printAsJSON(trade);
  
              if (sellOrder.quantity == 0) {
                  System.out.println("order fulfilled");
                  break;
              }
          }
  </code></pre>