Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java statement before lambda

I had this code previously which worked

private Runnable persist(Data data) {
    data.setComparisonTime(NO_COMPARISON_PERFORMED);
    return () -> {
        ...
    };
}

Later I refactored the code as I’d missed this before.

private Runnable persist(Data data) { 
    return () -> {
        data.setComparisonTime(NO_COMPARISON_PERFORMED);
        ...
    };
}

What is the difference between these two statements?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It matters for when the data.setComparisonTime is run. You have a function that returns a function; in the first case, the setComparisonTime will be run when the persist method is run. In the latter, it will not be run until the returned lambda is run.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading