10 Java Stream Filter Examples

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

In this blog post, we will demonstrate different ways to use the filter() method in Java streams to perform filtering operations based on various conditions.

1. Filtering even numbers:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbers = numbers.stream()
                                  .filter(n -> n % 2 == 0)
                                  .collect(Collectors.toList());
System.out.println(evenNumbers); // Output: [2, 4, 6, 8, 10]

2. Filtering strings of a specific length:

List<String> words = Arrays.asList("apple", "banana", "car", "dog", "elephant");
List<String> lengthThreeWords = words.stream()
                                     .filter(s -> s.length() == 3)
                                     .collect(Collectors.toList());
System.out.println(lengthThreeWords); // Output: [car, dog]

3. Filtering strings that start with a specific letter:

List<String> words = Arrays.asList("apple", "banana", "car", "dog", "elephant");
List<String> startsWithB = words.stream()
                               .filter(s -> s.startsWith("b"))
                               .collect(Collectors.toList());
System.out.println(startsWithB); // Output: [banana]

4. Filtering objects based on a condition:

List<Person> people = Arrays.asList(
    new Person("John", 25),
    new Person("Alice", 30),
    new Person("Bob", 20),
    new Person("Jane", 35)
);
List<Person> adults = people.stream()
                            .filter(p -> p.getAge() >= 18)
                            .collect(Collectors.toList());
System.out.println(adults); // Output: [John, Alice, Bob, Jane]

5. Filtering distinct elements:

List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 5, 3, 1);
List<Integer> distinctNumbers = numbers.stream()
                                       .distinct()
                                       .collect(Collectors.toList());
System.out.println(distinctNumbers); // Output: [1, 2, 3, 4, 5]

6. Filtering using multiple conditions:

List<Person> people = Arrays.asList(
    new Person("John", 25),
    new Person("Alice", 30),
    new Person("Bob", 20),
    new Person("Jane", 35)
);
List<Person> filteredPeople = people.stream()
                                   .filter(p -> p.getAge() >= 18 && p.getAge() <= 30)
                                   .collect(Collectors.toList());
System.out.println(filteredPeople); // Output: [John, Alice, Bob]

7. Filtering using a predicate:

List<String> words = Arrays.asList("apple", "banana", "car", "dog", "elephant");
Predicate<String> predicate = s -> s.length() > 3 && s.contains("a");
List<String> filteredWords = words.stream()
                                  .filter(predicate)
                                  .collect(Collectors.toList());
System.out.println(filteredWords); // Output: [apple, banana, elephant]

8. Filtering using a custom method:

List<String> words = Arrays.asList("apple", "banana", "car", "dog", "elephant");
List<String> filteredWords = words.stream()
                                  .filter(this::startsWithVowel)
                                  .collect(Collectors.toList());

private boolean startsWithVowel(String word) {
    return word.matches("[aeiouAEIOU].*");
}

System.out.println(filteredWords); // Output: [apple, elephant]

9. Filtering using negation:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> filteredNumbers = numbers.stream()
                                       .filter(n -> n % 2 != 0)
                                       .collect(Collectors.toList());
System.out.println(filteredNumbers); // Output: [1, 3, 5]

10. Filtering using a range of values:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> filteredNumbers = numbers.stream()
                                       .filter(n -> n > 5 && n < 9)
                                       .collect(Collectors.toList());
System.out.println(filteredNumbers); // Output: [6, 7, 8]

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare