This question already has an answer here:
- Why does Java8 Stream generate nothing? 3 answers
I am learning filtering using java stream. But the stream after filtering is not printing anything. I think the filter method is not getting executed. My filtering code is as follows:
Stream.of("d2", "a2", "b1", "b3", "c")
.filter(s -> {
s.startsWith("b");
System.out.println("filter: " + s);
return true;
});
There is no compilation error and no exception also. Any suggestion?
filter
is an intermediate operation, which will be executed only if the Stream pipeline ends in a terminal operation.For example :
As it is, your filter method is useless, since it always returns
true
, and thus performs no filtering.