The filtering()
method in Java, part of the java.util.stream.Collectors
class, is used to filter elements of a stream before collecting them. This method is useful when you need to apply a filter condition and then collect the filtered elements.
Table of Contents
- Introduction
filtering()
Method Syntax- Understanding
filtering()
- Examples
- Basic Usage
- Using
filtering()
with Multiple Conditions
- Real-World Use Case
- Conclusion
Introduction
The filtering()
method returns a Collector
that filters elements according to a specified predicate and then collects them. This method is particularly useful when you want to collect only the elements that match certain criteria.
filtering() Method Syntax
The syntax for the filtering()
method is as follows:
public static <T, A, R> Collector<T, ?, R> filtering(Predicate<? super T> predicate, Collector<? super T, A, R> downstream)
Parameters:
predicate
: APredicate
that tests each element for a condition.downstream
: ACollector
that collects the elements that match the predicate.
Returns:
- A
Collector
that filters elements according to the predicate and then collects them.
Throws:
- This method does not throw any exceptions.
Understanding filtering()
The filtering()
method allows you to filter the elements of a stream before collecting them. This is useful in scenarios where you need to collect only the elements that meet specific conditions, such as filtering out certain values before aggregating the results.
Examples
Basic Usage
To demonstrate the basic usage of filtering()
, we will create a list of integers, filter out the even numbers, and collect the remaining elements into a list.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FilteringExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
// Filter out even numbers and collect the remaining elements into a list
List<Integer> oddNumbers = numbers.stream()
.collect(Collectors.filtering(
n -> n % 2 != 0,
Collectors.toList()
));
System.out.println("Odd Numbers: " + oddNumbers);
}
}
Output:
Odd Numbers: [1, 3, 5]
Using filtering()
with Multiple Conditions
This example shows how to use filtering()
to filter elements based on multiple conditions.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FilteringWithMultipleConditionsExample {
public static void main(String[] args) {
List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "fig", "grape");
// Filter words that start with 'a' or have length greater than 4
List<String> filteredWords = words.stream()
.collect(Collectors.filtering(
word -> word.startsWith("a") || word.length() > 4,
Collectors.toList()
));
System.out.println("Filtered Words: " + filteredWords);
}
}
Output:
Filtered Words: [apple, banana, cherry, grape]
Real-World Use Case
Filtering and Collecting Names Starting with a Specific Letter
In real-world applications, the filtering()
method can be used to filter and collect names that start with a specific letter.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class NameFilteringExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");
// Filter and collect names that start with 'A'
List<String> namesStartingWithA = names.stream()
.collect(Collectors.filtering(
name -> name.startsWith("A"),
Collectors.toList()
));
System.out.println("Names Starting with 'A': " + namesStartingWithA);
}
}
Output:
Names Starting with 'A': [Alice]
Conclusion
The Collectors.filtering()
method is used to filter elements of a stream before collecting them. This method is particularly useful for collecting only the elements that match specific criteria. By understanding and using this method, you can efficiently manage filtering and collection operations in your Java applications.
Comments
Post a Comment
Leave Comment