in JAVA by (32.2k points)
What does the filter() method do? when you use it?

1 Answer

0 votes
by (32.2k points)
The filter method is used to filter elements that satisfy a certain condition that is specified using a Predicate function.

A predicate function is nothing but a function that takes an Object and returns a boolean. For example, if you have a List of Integer and you want a list of even integers.

In this case, you can use the filter to achieve that. You supply a function to check if a number is even or odd, just like this function and filter will apply this to stream elements and filter the elements which satisfy the condition and which doesn't.

Related questions

0 votes
asked Apr 15, 2021 in JAVA by SakshiSharma (32.2k points)
0 votes
asked May 31, 2022 in JAVA by Robin (14.6k points)
0 votes
0 votes
asked Feb 7, 2020 in JAVA by SakshiSharma (32.2k points)