I have a question about scope, streams, and reassignment.
I'm wondering if there's a way to do this using the streaming api without reassigning the list. the problem with reassigning the parameter is it breaks the scope, and so the test doesn't see the new value.
I know there are other ways (iterator, backwards iteration, etc.) to solve the problem. This question is just out of curiosity.
Test code:
@Test
void testAllNegativesByIterator() {
List<Integer> values = new ArrayList<>(Arrays.asList(-1, -2, -3, -4, -5, -6, -7, -8, -9));
p.removeNegativesByIterator(values);
checkNonNegativeSize(0, values);
}
Code under test:
public void removeNegativesByStream(List<Integer> values) {
values = values.stream().filter(v -> v > 0).collect(Collectors.toList());
}
Edit:
I also understand I could return a value. As I said in the question, I'm asking specifically a way to do this using streams out of curiosity.