40

I have a java 8 stream loop with the following content:

    void matchSellOrder(Market market, Order sellOrder) {
        System.out.println("selling " + market.pair() + " : " + sellOrder);

        market.buyOrders()
                .stream()
                .filter(buyOrder -> buyOrder.price >= sellOrder.price)
                .sorted(BY_ASCENDING_PRICE)
                .forEach((buyOrder) -> {
                    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
                    }
                });
    }

How can I break out of loop when some condition is met? Whats the right way to close stream anyway?

UPDATE

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:

        List<Order> applicableSortedBuyOrders = market.buyOrders()
                .stream()
                .filter(buyOrder -> buyOrder.price >= 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;
            }
        }
3
  • 1
    This question IS a duplicate, but not so much of the question indicated, but of this one: stackoverflow.com/questions/23308193/… Jul 9, 2015 at 9:36
  • I've not tried this but...What if you perform your work in a peek() consumer and follow it with an anyMatch() predicate on a concurrency-safe termination state object? Sep 1, 2017 at 8:12
  • 1
    you may place a sequential() before your terminal operation to avoid concurrency problems. then just replace forEach with anyMatch and return inside the predicate return sellOrder.quantity == 0
    – benez
    Sep 21, 2017 at 11:31

2 Answers 2

52

Stream.forEach is not a loop and it's not designed for being terminated using something like break. If the stream is a parallel stream the lambda body could be executed on different threads at the same time (not easy to break that and it could easily produce incorrect results).

Better use a iterator with a while loop:

Iterator<BuyOrderType> iter = market.buyOrders() // replace BuyOrderType with correct type here
            .stream()
            .filter(buyOrder -> buyOrder.price >= sellOrder.price)
            .sorted(BY_ASCENDING_PRICE).iterator();
while (iter.hasNext()) {
    BuyOrderType buyOrder = iter.next()  // replace BuyOrderType with correct type here
    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;
    }
}
9
  • 1
    thanks, seems i was misusing new feature.
    – vach
    Jun 2, 2014 at 14:52
  • P.S. both are of the same type "Order"
    – vach
    Jun 2, 2014 at 14:55
  • I agree with this answer, except for the first statement. Stream.forEach is a loop. The difference is that it's an internal loop, instead of an external loop. Because the loop is internal you cannot call break on it.
    – Bogoth
    Oct 5, 2015 at 16:47
  • 2
    o ye of little faith! you totally can break out of lambda loop: stream.allMatch(value -> { if (...) return true; /* continue */ else return false; /* break */ });
    – yk4ever
    Jun 30, 2016 at 10:57
  • 3
    @yk4ever : Tell this to a parallel steam! E.g. IntStream.range(0, 1000).boxed().parallel().allMatch(i -> { System.out.println(i); return i % 2 != 0; });
    – fabian
    Jun 30, 2016 at 11:15
28

Well, there is no method to do this in the stream api, (as far as i know).

But if you really need it, you can use an Exception.

EDIT: For the people giving -1 to this answer I'm not advertising this as an approach one should follow, it's just an option for the cases where you need it, and it does answer the question.

public class BreakException extends RuntimeException {...}

try {
    market.buyOrders()
            .stream()
            .filter(buyOrder -> buyOrder.price >= sellOrder.price)
            .sorted(BY_ASCENDING_PRICE)
            .forEach((buyOrder) -> {
                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");
                    throw new BreakException()
                }
            });
} catch (BreakException e) {
    //Stoped
}
3
  • 6
    This is an option too, but as far as I know throwing and handilng exception are too expensive operations in terms of performance and should be avoided (as well as finally blocks mentioned above)
    – vach
    Jun 2, 2014 at 14:53
  • 1
    Won't work with parallel streams.
    – rudi
    May 14, 2019 at 13:09
  • Using exceptions to control program flow is not a good practice.
    – clapas
    Nov 7, 2022 at 12:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.