About count()

Brian Goetz brian.goetz at oracle.com
Wed Aug 29 04:42:25 PDT 2012


I think "force evaluation" is probably the wrong way to look at it.

If you've got a chain:

   int sum = collection.stream()
                       .filter(...)
                       .map(...)
                       .reduce(...);

The filtering, mapping, and reducing are all happening at the same time. 
  If, for debugging purposes, you want to see the values go by at any 
point, you don't need to force any evaluation, you just need to insert a 
tap in the pipeline:

   int sum = collection.stream()
                       .filter(...)
                       .tee(e -> System.out.printf("Filtered: %s%n", e)
                       .map(...)
                       .reduce(...);

This won't change the evaluation, but you can see the filtered values go by.

If you want to first do all the filtering, then do all the mapping, you 
can rewrite as:

   List<Foo> filtered = collection.stream()
                                  .filter(...)
                                  .into(new ArrayList<>());
   List<Moo> mapped = filtered.stream()
                              .map(...)
                              .into(new ArrayList<>());
etc.


On 8/29/2012 2:25 AM, Jose wrote:
>
> But, with the current implementation, what is the cheapest way to force
> evaluation at certain points of the the chain for debugging porpouses?
>
>> As mentioned earlier, we're migrating away from putting extensions on
>> terable, in part for reasons like this.
>
>


More information about the lambda-dev mailing list