title: How to make a Stream from a For Loop
body: <p>I have a for loop that gets values passed through argument and if certain condition is meet, it creates a new block object. I need to transform this loop to Stream. I search another cases like this, but as the condition of this loop is not a value of the iterator i, but a date condition, I couldn't figure it out how to limit the stream. Also, the items are not premade, like in other examples, but they're being made in the progress. Here is the code:</p>
  
  <pre><code>class DateBlock {    
      final Instant from;
      final Instant to;
  
      DateBlock(Instant from, Instant to) {
          this.from = from;
          this.to = to;
      }
  
      public static List&lt;DateBlock&gt; blockize(ZonedDateTime fromDate, 
              ZonedDateTime toDate, 
              int blockSize, 
              ChronoUnit blockSizeUnit) {
          List&lt;DateBlock&gt; blocks = new ArrayList&lt;&gt;();
          boolean reachedDate = false;
          for (int i = 0; !reachedDate; i++) {
              ZonedDateTime minDate = fromDate.plus(i * blockSize, blockSizeUnit);
              ZonedDateTime maxDate = fromDate.plus((i + 1) * blockSize, blockSizeUnit);
              reachedDate = toDate.isBefore(maxDate);
              blocks.add(new DateBlock(minDate.toInstant(), maxDate.toInstant()));
          }
  
          return blocks;
      }
  }
  </code></pre>
  
  <p>How I need to proceed?</p>