Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Sneak Peak: Method References

Thomas Lian Ødegaard
Thomas Lian Ødegaard
9,540 Points

I have a problem understanding Lambdas outside sorting

I do understand what the lambda does in this workshop, how it sorts the book title. But in the Pomodoro timer. We used example this one:

        mTimeLine.getKeyFrames().add(new KeyFrame(Duration.seconds(1), e -> {
            mCurrentAttempt.tick();
            setTimerText(mCurrentAttempt.getRemainingSeconds());
        }));

and this one

        mTimeLine.setOnFinished(e -> {
            saveCurrentAttempt();
            mApplause.play();
            prepareAttempt(mCurrentAttempt.getKind() == AttemptKind.FOCUS ?
                    AttemptKind.BREAK : AttemptKind.FOCUS);
        });

How to they work?

2 Answers

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey Thomas,

with Lambdas you are basically extending an interface that only defines one method.

someMethod(
    (e) -> { 
        doSomething; 
    }
);

This code means: Create a new instance of a class that implements the interface that 'someMethod(SomeInterface interface)' is expecting, by implementing the one method that is defined in the interface. Pass in 'e' as the argument for that method and 'doSomething;' in the method body.

Regards, Florian

Hi Thomas,

Lambdas allow you to define single-method classes in a more convenient and compact way; a way that doesn't require you to create a new instance; it basically eliminates the need to create a new class object and allocate resources for it.

Perhaps breaking down the information could help. In the workshop, there were (4) objects used to demonstrate lambda: those are Collections, Comparator, Book, and List. And the Collections' method utilizes objects (the "books") contained within List, so there are some dependencies which serves the purpose of the workshop to demonstrate lambdas in (5) different ways.

In comparison to your example here, the closest code pattern from the workshop that you can study is the one used in usingLambdasInLongForm() method. Your example, and this workshop's example, both follow the pattern below:

<objectOrMethodName>( <parameterName1>, <parameterName2> -> {
         doThisForParameter2();
         doThatForParameter2();
} );

In your first example, mTimeLine.getKeyFrames() represents an object, and the fact that it used a method called getKeyFrames() to call out the "keyframe" (or the object returned by the getKeyFrames() method) of mTimeLine, is perhaps where the confusion is coming from, since the lambda expression here is actually implemented inside the add() method.

So in equivalence to the Pomodoro timer,

  1. objectOrMethodName = new KeyFrame()
  2. parameterName1 = Duration.seconds(1)
  3. parameterName2 = e
  4. doThisForParameter2() = mCurrentAttempt.tick();
  5. doThatForParameter2() = setTimerText(mCurrentAttempt.getRemainingSeconds());

Hope it helped :)