I want to split a string by "||" and count how many elements from the resulting array return true for a function boolean isValid(String[] r).
I'm trying to use Arrays.stream to evaluate and filter the array, and finally, filter the resulting array to only keep the true values.
boolean[] truthSet = Arrays.stream(range.split("\\s+))
.map(r -> isValid(r))
.filter(b -> _whatGoesHere_)
.toArray(boolean[]::new);
In place of _whatGoesHere, I tried putting b, b == true, to no avail. What's the right way to achieve this?
b -> b. You should read it. And post it. It has to do with trying to make an array ofboolean(primitive types) from aStream<java.lang.Boolean>(objects). What's the point in creating an array with only true values anyway? Wouldn't the size of the stream convey the same information? (i.e. there are N valid strings)?mapthe elements; you can usefilterdirectly:Arrays.stream(...).filter(str -> isValid(str)).count().