The Java Stream API that was introduced in Java 8 is the most powerful API when it comes to processing objects in a collection.
One of the most used methods from this API is the filter(Predicate predicate)
You can definitely make use of the filter method multiple times while processing the stream. Let's see some examples.
Example 1:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class MultipleFilterStreamExample {
public static void main(String[] args) {
List<String> namesList = new ArrayList<>();
namesList.add("Mike");
namesList.add("Alan");
namesList.add("Aon");
namesList.add("Alen");
namesList.add("Bob");
namesList.add("Andrew");
namesList.add("Kate");
List<String> filteredNameList = namesList.stream()
.filter(name -> name.startsWith("A"))
.filter(name -> name.endsWith("n"))
.collect(Collectors.toList());
System.out.println(filteredNameList);
}
}
In the above example we have made use of a chain of filter method to narrow the list to all names that start with "A" or end with "n"
Example 2:
package org.code2care.threading;
public class Employee {
private int empId;
private String empName;
private int empAge;
private int empSal;
public Employee(int empId, String empName, int empAge, int empSal) {
this.empId = empId;
this.empName = empName;
this.empAge = empAge;
this.empSal = empSal;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpAge() {
return empAge;
}
public void setEmpAge(int empAge) {
this.empAge = empAge;
}
public int getEmpSal() {
return empSal;
}
public void setEmpSal(int empSal) {
this.empSal = empSal;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", empAge=" + empAge +
", empSal=" + empSal +
'}';
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamFilterChainingExample {
public static void main(String[] args) {
Employee employee1 = new Employee(1,"Alex",22,54000);
Employee employee2 = new Employee(2,"Alen",25,30000);
Employee employee3 = new Employee(3,"Mike",26,38000);
List<Employee> employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);
List<Employee> employeeFilteredList = employeeList.stream()
.filter(employee -> employee.getEmpSal() > 40000)
.filter(employee -> employee.getEmpName().startsWith("A"))
.filter(employee -> employee.getEmpAge() > 20)
.collect(Collectors.toList());
System.out.println(employeeFilteredList);
}
}
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Android : Exception raised during rendering: action_bar API 22 - Android
- Limit scrollback rows in macOS Terminal - MacOS
- Setup Git + Visual Studio Code Tutorial - Git
- Python: How to POST Json Data with HTTP Request - Python
- Correct way to Get the Current Date in Java 8 or above - Java
- Installing Google Cloud macOS SDK - Google
- Calculate Area of ellipse - C-Program
- How to use SSH in Windows Terminal - Windows