Java 8 Stream IllegalStateException: Stream has already been operated on or closed

前端 未结 6 675
孤独总比滥情好
孤独总比滥情好 2020-11-27 07:45

I\'m trying to generate Order instances using the Stream API. I have a factory function that creates the order, and a DoubleStream is used to initialize the amount of the o

6条回答
  •  难免孤独
    2020-11-27 08:19

    Thank you - that was very helpful. I also came up with a different implementation that works well for now:

    private DoubleStream doubleStream = new Random().doubles(50.0, 200.0);
    
    private List createOrders(int numberOfOrders) {
    List orders = new ArrayList<>();
    doubleStream.limit(numberOfOrders).forEach((value) -> {
        Order order = new Order(value);
        orders.add(order);
    });
    return orders;
    }
    

    Thanks again!

    Ole

提交回复
热议问题