how to add multiple filter condition in Java stream filter chain
Java
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Main
{
public static void main(String[] args)
{
List<Employee> employeeList = getEmployeesFromDataSource();
//filter 1
Predicate<Employee> isEmployeeActive = e -> e.getStatus() == EmployeeStatus.ACTIVE;
//filter2
Predicate<Employee> isAccountActive = e -> e.getAccount().getStatus() == AccountStatus.ACTIVE;
//Active employees
String result = employeeList.stream()
.filter(isEmployeeActive)
.map(e -> e.getId().toString())
.collect(Collectors.joining(",", "[", "]"));
System.out.println("Active employees = " + result);
//Active employees with active accounts
result = employeeList.stream()
.filter(isEmployeeActive.and(isAccountActive))
.map(e -> e.getId().toString())
.collect(Collectors.joining(",", "[", "]"));
System.out.println("Active employees with active accounts = " + result);
//Active employees with inactive accounts
result = employeeList.stream()
.filter(isEmployeeActive.and(isAccountActive.negate()))
.map(e -> e.getId().toString())
.collect(Collectors.joining(",", "[", "]"));
System.out.println("Active employees with inactive accounts = " + result);
//Inactive employees with inactive accounts
result = employeeList.stream()
.filter(isEmployeeActive.negate().and(isAccountActive.negate()))
.map(e -> e.getId().toString())
.collect(Collectors.joining(",", "[", "]"));
System.out.println("Inactive employees with inactive accounts = " + result);
}
private static List<Employee> getEmployeesFromDataSource() {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee(1L, "A", "AA", EmployeeStatus.ACTIVE,
new Account(1001L, "Saving - 1001", "Saving", AccountStatus.ACTIVE)));
employeeList.add(new Employee(2L, "B", "BB", EmployeeStatus.ACTIVE,
new Account(1002L, "Checking - 1002", "Checking", AccountStatus.ACTIVE)));
employeeList.add(new Employee(3L, "C", "CC", EmployeeStatus.ACTIVE,
new Account(1003L, "Deposit - 1003", "Deposit", AccountStatus.ACTIVE)));
employeeList.add(new Employee(4L, "D", "DD", EmployeeStatus.ACTIVE,
new Account(1004L, "Saving - 1004", "Saving", AccountStatus.INACTIVE)));
employeeList.add(new Employee(5L, "E", "EE", EmployeeStatus.ACTIVE,
new Account(1005L, "Checking - 1005", "Checking", AccountStatus.INACTIVE)));
employeeList.add(new Employee(6L, "F", "FF", EmployeeStatus.ACTIVE,
new Account(1006L, "Deposit - 1006", "Deposit", AccountStatus.BLOCKED)));
return employeeList;
}
}
Also in Java:
- Title
- How tomake teris in Java
- Category
- Java
- Title
- trim() and split() in java
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- java script how to not allow soace
- Category
- Java
- Title
- android bottom navigation hiding views
- Category
- Java
- Title
- pascals triangle java
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- arraylist with values
- Category
- Java
- Title
- set iteration java
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- fibonacci series i logn java
- Category
- Java
- Title
- read from elasticsearch in spark
- Category
- Java
- Title
- how to append to an array in java
- Category
- Java
- Title
- java map foreach
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- counting repeated characters in a string in java
- Category
- Java
- Title
- java program to get all cat indices in elasticsearch javadoc
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- eliminar fila jtable java
- Category
- Java
- Title
- que es un length en java
- Category
- Java
- Title
- how to show the hex detail of a file in java
- Category
- Java
- Title
- how to exit a for loop in java
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- simple calculator program in java
- Category
- Java
- Title
- java how to iterate through a arraylist
- Category
- Java
- Title
- snackbar not showing with bottom nav
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- compile java
- Category
- Java
- Title
- how to print to the console in java
- Category
- Java
- Title
- jsonobject to java object
- Category
- Java
- Title
- change activity main drawer items text color programmatically android
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- how to make a copy of an array java
- Category
- Java
- Title
- for loop in java stack overflow
- Category
- Java
- Title
- spannable string in android java for color
- Category
- Java
- Title
- javafx detect collision
- Category
- Java
- Title
- how to open a webpage with java
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- bufferedwriter doesn't write to file
- Category
- Java
- Title
- how to create a list in java
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- setting up javafx in eclipse
- Category
- Java
- Title
- how to compare strings java
- Category
- Java
- Title
- java how to compare strings
- Category
- Java
- Title
- initiralizing an arraylist java
- Category
- Java
- Title
- labeled for loop in java
- Category
- Java
- Title
- java set example
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- print out value java
- Category
- Java
- Title
- system.arraycopy java
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- initialize applet in java
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- how to use beacon power in minecraft in java edition
- Category
- Java
- Title
- java if statement string equals scanner
- Category
- Java
- Title
- Please ensure that your JAVA_HOME points to a valid Java SDK. You are currently pointing to: /usr/lib/jvm/java-13-oracle
- Category
- Java
- Title
- java print treemap
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- dataframe to dict without index
- Category
- Java
- Title
- hibernate onetone with mapsid
- Category
- Java
- Title
- run static method java
- Category
- Java
- Title
- java 8 stream group by example
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- how to get the dimensions of a 2d array in java
- Category
- Java
- Title
- prendere valore da tastiera java
- Category
- Java
- Title
- copy array in java
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- Java nested array hashcode
- Category
- Java
- Title
- how to echo java_home in windows cmd
- Category
- Java
- Title
- java get command line output
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- how to get a user input in java
- Category
- Java
- Title
- print arraylist java
- Category
- Java
- Title
- java import text file into arraylist
- Category
- Java
- Title
- java hashmap put
- Category
- Java
- Title
- how to fill a list with a single value java
- Category
- Java
- Title
- shortcut to find a class in java project eclipse
- Category
- Java
- Title
- charat(0).touppercase() java
- Category
- Java
- Title
- import
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- how to bubblesort a string array in java
- Category
- Java
- Title
- binary to int java
- Category
- Java