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))
Share:
11,211

Related videos on Youtube

Shan
Author by

Shan

Updated on June 04, 2022

Comments

  • Shan
    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
      Holger over 6 years
      Use a lambda expression. Unless your statusList truly is a list of lists, even the first parameter doesn’t match.
    • Shan
      Shan over 6 years
      statusList is having the list of statuses
    • JB Nizet
      JB Nizet over 6 years
      Then 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
      Shan over 6 years
      statusList is getting passed. Above code is working already with getReportDetails(List statusList). when i add 1 more parameter, unable to pass inside map, it throws error
    • shmosel
      shmosel over 6 years
      Impossible. There's something you're not telling us.
    • Andreas
      Andreas over 6 years
      Where 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
      Shan over 6 years
      sorry, i forgot to mention type of list and id
    • fps
      fps over 6 years
      Beyond the question, you shouldn't use forEach to put elements in a map. Use collect(Collectors.toMap(...)) instead.
    • JGFMK
      JGFMK almost 4 years
      I wonder if this could be done with a Runnable?
  • Andreas
    Andreas over 6 years
    You should show dummy declaration of someId to highlight that it has to come from somewhere, e.g. String someId = /*assign value*/;
  • JB Nizet
    JB Nizet over 6 years
    @Andreas indeed. Done.