🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
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:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment