I face the same problem often. I need to count the runs of a lambda for use outside the lambda. E.g.:
myStream.stream().filter(...).forEach(item->{ ... ; runCount++);
System.out.println("The lambda ran "+runCount+"times");
The issue is that runCount needs to be final, so it cannot be an int. It cannot be an Integer because that's immutable. I could make it class level variable (i.e. a field) but I'll only need it in this block of code. I know there are various ways, I'm just curious what is your preferred solution for this? Do you use an AtomicInteger or an array reference or some other way?
If you don't want to create a field because you only need it locally, you can store it in an anonymous class:
Weird, I know. But it does keep the temporary variable out of even local scope.
Another way of doing this (useful if you'd like your count to only be incremented in some cases, like if an operation was successful) is something like this, using
mapToInt()
andsum()
:As Stuart Marks noted, this is still somewhat odd, because it's not completely avoiding side effects (depending on what
foo()
andbar()
are doing).And another way of incrementing a variable in a lambda that's accessible outside of it is to use a class variable:
In this example, using a class variable for a counter in one method probably doesn't make sense, so I'd caution against it unless there's a good reason to. Keeping class variables
final
if possible can be helpful in terms of thread safety, etc (see http://www.javapractices.com/topic/TopicAction.do?Id=23 for a discussion on usingfinal
).To get a better idea of why lambdas work the way they do, https://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood has a detailed look.
Let me reformat your example a bit for the sake of discussion:
If you really need to increment a counter from within a lambda, the typical way to do so is to make the counter an
AtomicInteger
orAtomicLong
and then call one of the increment methods on it.You could use a single-element
int
orlong
array, but that would have race conditions if the stream is run in parallel.But notice that the stream ends in
forEach
, which means that there is no return value. You could change theforEach
to apeek
, which passes the items through, and then count them:This is somewhat better, but still a bit odd. The reason is that
forEach
andpeek
can only do their work via side effects. The emerging functional style of Java 8 is to avoid side effects. We did a little of that by extracting the increment of the counter into acount
operation on the stream. Other typical side effects are adding items to collections. Usually these can be replaced via use of collectors. But without knowing what actual work you're trying to do, I can't suggest anything more specific.As an alternative to sync hassling AtomicInteger one could use an integer array instead. As long as the reference to the array does not get another array assigned (and that's the point) it can be used as a final variable while the values of the fields can change arbitrarily.