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);
}
}
Have Questions? Post them here!
- Create a Zip file using Java Code programmatically
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- How to Sort a LinkedList in Java
- Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver
- How to declare and initialize Array in Java Programming
- [Fix] java: integer number too large compilation error
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Reading .xls and .xlsx Excel file using Apache POI Java Library
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- How to get Client IP address using Java Code Example
- Truncate table using Java JDBC Example
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- How to get file path in Idea IntelliJ IDE
- Java Generics explained with simple definition and examples
- Java SE 8 Update 301 available with various bug fixes and security improvements
- Java: Collect Stream as ArrayList or LinkedList
- Java JDBC Connection with PostgreSQL Driver Example
- How to check if Java main thread is alive
- How to fix Java nio NoSuchFileException wile reading a file
- Java 8+ get Day of the Week Examples with LocalDateTime, DateTime, ZonalDateTime and Instant classes
- Ways to Convert Integer or int to Long in Java
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Fix] Spring Boot: mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
- Java: The value of the local variable string is not used
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Chrome : When Adobe flash player has finished updating, reload this page to active it - Chrome
- Calculate Volume of Sphere - C-Program
- Increase Font Size of Eclipse Java Code - Eclipse
- Linux: Create a New User and Password and Login Example - Linux
- Programmatically check if Facebook is installed on Android device - Android
- Delete blank lines in a file using Notepad++ - NotepadPlusPlus
- Fix Power BI error Access to the resource is forbidden when connecting SharePoint Online List as data source - SharePoint
- How to make Android EditText not editable - Android