I am using a functional programming style to solve the Leetcode easy question, Count the Number of Consistent Strings. The premise of this question is simple: count the amount of values for which the predicate of "all values are in another set" holds.
I have two approaches, one which I am fairly certain behaves as I want it to, and the other which I am less sure about. Both produce the correct output, but ideally they would stop evaluating other elements after the output is in a final state.
public int countConsistentStrings(String allowed, String[] words) {
final Set<Character> set = allowed.chars()
.mapToObj(c -> (char)c)
.collect(Collectors.toCollection(HashSet::new));
return (int)Arrays.stream(words)
.filter(word ->
word.chars()
.allMatch(c -> set.contains((char)c))
)
.count();
}
In this solution, to the best of my knowledge, the allMatch statement will terminate and evaluate to false at the first instance of c for which the predicate does not hold true, skipping the other values in that stream.
public int countConsistentStrings(String allowed, String[] words) {
Set<Character> set = allowed.chars()
.mapToObj(c -> (char)c)
.collect(Collectors.toCollection(HashSet::new));
return (int)Arrays.stream(words)
.filter(word ->
word.chars()
.mapToObj(c -> set.contains((char)c))
.reduce((a,b) -> a&&b)
.orElse(false)
)
.count();
}
In this solution, the same logic is used but instead of allMatch
, I use map
and then reduce
. Logically, after a single false
value comes from the map
stage, reduce
will always evaluate to false
. I know Java streams are lazy, but I am unsure when they ''know'' just how lazy they can be. Will this be less efficient than using allMatch
or will laziness ensure the same operation?
Lastly, in this code, we can see that the value for x
will always be 0 as after filtering for only positive numbers, the sum of them will always be positive (assume no overflow) so taking the minimum of positive numbers and a hardcoded 0 will be 0. Will the stream be lazy enough to evaluate this to 0 always, or will it work to reduce every element after the filter anyways?
List<Integer> list = new ArrayList<>();
...
/*Some values added to list*/
...
int x = list.stream()
.filter(i -> i >= 0)
.reduce((a,b) -> Math.min(a+b, 0))
.orElse(0);
To summarize the above, how does one know when the Java stream will be lazy? There are lazy opportunities that I see in the code, but how can I guarantee that my code will be as lazy as possible?
reduce
doesn’t know what function you’re reducing - it’s an arbitrary “chunk of code”. It definitely doesn’t know what the value you pass as the default toOptional
. So no, the second approach is not lazy.reduce
reduces to a a single value and for that it needs to inspect all values (what Boris the Spider already commented). Furthermore,reduce
is a terminal operation and as such consumes the stream, which is quite the opposite of lazy.filter
,map
and any other intermediate operation are lazy: they are only evaluated when the first terminal operation is called.reduce
, the function passed to reduce contains&&
,Math.min
, or+
which are evaluated eagerly. As such, Java cannot ignore part of those function calls.allMatch
is also a terminal operation but still lazy and short-circuiting.allMatch
andreduce
is not the laziness but that the former is short-circuiting. That difference is what the question is about.