Java 8 Stream and filter is not working on list
Solution 1
You have mistake in predicate body. It should be.
completed=progressList.stream().filter(p -> p.getCaseExternalStatusL1().equalsIgnoreCase("Completed")).collect(Collectors.toList());
since otherwise you just search for Boolean in List. Other places should be changed too.
Solution 2
To take just your first filter:
.filter(p -> progressList.contains(p.getCaseExternalStatusL1().equalsIgnoreCase("Completed")))
In this, you have
p.getCaseExternalStatusL1().equalsIgnoreCase("Completed")
which is a boolean, and you are checking if progressList
contains that boolean. As it is a list of ProgressSheet
, it is not going to contain a boolean. Your predicate does not make sense.
Possibly you mean:
.filter(p -> p.getCaseExternalStatusL1().equalsIgnoreCase("Completed"))
which means "include only values p
where
p.getCaseExternalStatusL1().equalsIgnoreCase("Completed")
is true"
Related videos on Youtube
user1111880
Updated on September 14, 2022Comments
-
user1111880 over 1 year
In below code, I need to filter list based on 1 property called as 'CaseExternalStatusL1'. As I don't want to write needless code so I am trying to use Java 8 stream and filter with Lambda expression. When I am trying to run code all the list (inProgress,completed,pending) are still showing their size as 0 where as I am explicitly setting each object for each list.
Can you guys tell me what I am doing wrong.
public void saveProgressSheet(List<ProgressSheet> progressList) throws BusinessException { List<ProgressSheet> inProgress = new ArrayList<ProgressSheet>(); List<ProgressSheet> completed = new ArrayList<ProgressSheet>(); List<ProgressSheet> pending = new ArrayList<ProgressSheet>(); List<EmpInitiated> empInitiatedList = new ArrayList<EmpInitiated>(); completed=progressList.stream() .filter(p -> progressList.contains(p.getCaseExternalStatusL1().equalsIgnoreCase("Completed"))) .collect(Collectors.toList()); inProgress =progressList.stream() .filter(p -> progressList.contains(p.getCaseExternalStatusL1().equalsIgnoreCase("Work In Progress"))) .collect(Collectors.toList()); pending =progressList.stream() .filter(p -> progressList.contains(p.getCaseExternalStatusL1().equalsIgnoreCase("final report sent case open"))) .collect(Collectors.toList()); }
-
user1111880 over 8 yearsYes, I mean include only value of p when value of p.getCaseExternalStatusL1().equalsIgnoreCase("Completed")