I have code:
static void doSmth() {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add(String.valueOf(i));
}
list.stream().filter("1329"::contains).map(s -> s + "a").forEach(System.out::println);
}
Why i got:
1a
2a
3a
9a
13a
29a
I expected a empty output, because list doesn't contains "1329".
Stream filter(Predicate predicate) returns a stream consisting of the elements of this stream that match the given predicate. This is an intermediate operation.
Stream. filter() is a method in Java we use while working with streams. It traverses through all the elements present and removes or filters out all those elements that are not matching with the specified condition through a significant argument.
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both. Filters perform filtering in the doFilter method.
After populating both the lists, we simply pass a Stream of Employee objects to the Stream of Department objects. Next, to filter records based on our two conditions, we're using the anyMatch predicate, inside which we have combined all the given conditions. Finally, we collect the result into filteredList.
because
.filter("1329"::contains)
mean
.filter(s -> "1329".contains(s))
not
.filter(s -> s.contains("1329"))
As I guess you think it means.
So your list hold :
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ... 25, 26, 27, 28, 29]
^ ^ ^ ^ ^ ^
Which "1329"
contains 1,2, 3, 9, 13 and 29
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With