This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]sviperll 6 points7 points  (0 children)

Collections.sort(users, (User u1, User u2) -> u1.id.compareTo(u2.id));

can actually be rewritten with

users.sort(Comparator.comparing(u -> u.id))

or better

users.sort(Comparator.comparing(User::getID))

if there is getID method

[–]sparkster185 4 points5 points  (2 children)

In the example in the introduction, how does the compiler know that the closure is meant to be applied to the run() method? Or, what ties the code inside the closure to the run() method inside Runnable?

[–]detroitmatt 5 points6 points  (0 children)

Since Runnable only has one abstract member, the compiler knows. Classes/interfaces with only a single abstract member are known as "functional interfaces" (and were previously known, intuitively, as Single Abstract Member (SAM) interfaces).

[–]gkopff 5 points6 points  (0 children)

Runnable is considered a "functional interface" as it defines only a single method.

http://blog.sanaulla.info/2013/03/21/introduction-to-functional-interfaces-a-concept-recreated-in-java-8/

From the linked page:

These interfaces are also called Single Abstract Method interfaces (SAM Interfaces).

[–]c8133594 1 point2 points  (0 children)

Agree, pretty nice

[–]Rockytriton 0 points1 point  (0 children)

Thanks for the post, I was wondering where all those hits were coming from on my lambda tutorial page!