How to fix this 'Lambdas should be replaced with method references' sonar issue in java 8?

uma :
public static Set<NurseViewPrescriptionWrapper> create(final Set<NurseViewPrescriptionDTO> nurseViewPrescriptionDTOs) {
  return nurseViewPrescriptionDTOs.stream()
      .map(new Function<NurseViewPrescriptionDTO, NurseViewPrescriptionWrapper>() {
        @Override
        public NurseViewPrescriptionWrapper apply(NurseViewPrescriptionDTO input) {
          return new NurseViewPrescriptionWrapper(input);
        }
      })
      .collect(Collectors.toSet());
}

I convert above code to java 8 lamda function as below.

public static Set<NurseViewPrescriptionWrapper> create(final Set<NurseViewPrescriptionDTO> nurseViewPrescriptionDTOs) {
  return nurseViewPrescriptionDTOs.stream()
      .map(input -> new NurseViewPrescriptionWrapper(input))
      .collect(Collectors.toSet());
}

Now, I am receiving sonar issue, like Lambdas should be replaced with method references , to '->' this symbol. How i can fix this issue ?

sweet suman :

Your lambda,

.map(input -> new NurseViewPrescriptionWrapper(input))

can be replaced by

.map(NurseViewPrescriptionWrapper::new)

That syntax is a method reference syntax. In the case of NurseViewPrescriptionWrapper::new is a special method reference that refers to a constructor

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=75597&siteId=1