2

I am trying to extract a filtered list on top of the original list based on some condition. I am using backport version of Java 8 and am not pretty sure how to do this.I get the Set from ccarReport.getCcarReportWorkflowInstances() call. I need to iterate and filter this set based on a condition match( I am comparing the date attribute in each object with the request date being passed. Below is the code

  Set<CcarReportWorkflowInstance> ccarReportWorkflowInstanceSet = ccarReport.getCcarReportWorkflowInstances();
  List<CcarReportWorkflowInstance> ccarReportWorkflowInstances = StreamSupport.stream(ccarReportWorkflowInstanceSet).filter(ccarReportWorkflowInstance -> DateUtils.isSameDay(cobDate, ccarReportWorkflowInstance.getCobDate()));

The routine which is doing the job

  public List<CcarRepWfInstDTO> fetchReportInstances(Long reportId, Date cobDate) {
    List<CcarRepWfInstDTO> ccarRepWfInstDTOs = null;
    CcarReport ccarReport = validateInstanceSearchParams(reportId, cobDate);
    Set<CcarReportWorkflowInstance> ccarReportWorkflowInstanceSet = ccarReport.getCcarReportWorkflowInstances();
    List<CcarReportWorkflowInstance> ccarReportWorkflowInstances = StreamSupport.stream(ccarReportWorkflowInstanceSet).filter(ccarReportWorkflowInstance -> DateUtils.isSameDay(cobDate, ccarReportWorkflowInstance.getCobDate()));
    ccarRepWfInstDTOs = ccarRepWfInstMapper.ccarRepWfInstsToCcarRepWfInstDTOs(ccarReportWorkflowInstances);
    return ccarRepWfInstDTOs;
}

Error I get when I tried to use streams.

enter image description here

1 Answer 1

3

Assuming I understood what you are trying to do, you can replace your method body with a single line :

return 
  validateInstanceSearchParams(reportId, cobDate).getCcarReportWorkflowInstances()
                                                 .stream()
                                                 .filter(c -> DateUtils.isSameDay(cobDate, c.getCobDate()))
                                                 .collect(Collectors.toList());
  1. You can obtain a Stream from the Set by using the stream() method. No need for StreamSupport.stream().
  2. After filtering the Stream, you should collect it into the output List.
  3. I'd use shorter variable and method names. Your code is painful to read.
Sign up to request clarification or add additional context in comments.

7 Comments

its throwing a compile time error saying it cannot return a custom object but only of Type<T>
@Balaji On which method call do you get this error?
I have attached the image containing the error as part of question.Please have a look
@Balaji: what’s the problem in simply writing the name of the method? That’s less effort than creating a screenshot.
@Balaji: Well, that’s exactly the problem. You are mixing two entirely different APIs. Java 8’s standard collector can’t implement your custom collector interface. Of course, it’s unfortunate that both have the same simple name. Look at the beginning of the package names in the error message…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.