pass two parameters using java8 stream().map to call service method
11,211
Start by not using raw types:
List<ReportCount> getReportDetails(List<String> statusList, String id);
Then use a lambda expression:
List<List<String>> listOfStatusList = new ArrayList<>();
listOfStatusList.add(Arrays.asList("SUCCESS"));
listOfStatusList.add(Arrays.asList("CANCELLED"));
listOfStatusList.add(Arrays.asList("ON_HOLD","INPROGRESS"));
String someId = ...;
listOfStatusList.stream()
.map(statusList -> reportService.getReportDetails(statusList, someId))
Related videos on Youtube
Author by
Shan
Updated on June 04, 2022Comments
-
Shan almost 2 years
I've the below service which works correctly with
Java8
List<ReportCount> getReportDetails(List<String> statusList); statusList.add(Arrays.asList("SUCCESS")); statusList.add(Arrays.asList("CANCELLED")); statusList.add(Arrays.asList("ON_HOLD","INPROGRESS")); statusList.stream() .map(reportService::getReportDetails) .forEach(e -> reportMap.put("report_" + reportMap.size(), e));
How to pass 1 more parameter using above code for below method.
String id="CA"; List<ReportCount> getReportDetails(List<String> statusList, String id);
Could anyone please help. Thanks
-
Holger over 6 yearsUse a lambda expression. Unless your
statusList
truly is a list of lists, even the first parameter doesn’t match. -
Shan over 6 yearsstatusList is having the list of statuses
-
JB Nizet over 6 yearsThen you want to pass the whole list, not each element, to getReportDetails(). Why are you using raw types? What it the type of the first argument of getReportDetails()?
-
Shan over 6 years
statusList
is getting passed. Above code is working already withgetReportDetails(List statusList)
. when i add 1 more parameter, unable to pass inside map, it throws error -
shmosel over 6 yearsImpossible. There's something you're not telling us.
-
Andreas over 6 yearsWhere is the
id
parameter value supposed to come from? How do you expect us to help with the syntax, when you don't even tell us where the value is? -
Shan over 6 yearssorry, i forgot to mention type of list and id
-
fps over 6 yearsBeyond the question, you shouldn't use
forEach
to put elements in a map. Usecollect(Collectors.toMap(...))
instead. -
JGFMK almost 4 yearsI wonder if this could be done with a Runnable?
-
-
Andreas over 6 yearsYou should show dummy declaration of
someId
to highlight that it has to come from somewhere, e.g.String someId = /*assign value*/;
-
JB Nizet over 6 years@Andreas indeed. Done.