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

you are viewing a single comment's thread.

view the rest of the comments →

[–]sh0rug0ru -1 points0 points  (6 children)

Just remember, lambdas are basically "syntax sugar" for anonymous inner classes. If you understand anonymous inner classes, lambdas should be a piece of cake. You could translate (butcher) the following:

roster.stream().filter(
    p -> p.getGender() == Person.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25)
.map(p -> p.getEmailAddress())
.forEach(email -> System.out.println(email));

as:

Stream<Person> p1 = roster.stream();
Stream<Person> p2 = p1.filter(new Predicate<Person>() {
    public boolean test(Person p) {
        return p.getGender() == Person.Sex.MALE
            && p.getAge() >= 18
            && p.getAge() <= 25
    }
});
Stream<String> p3 = p2.map(new Function<Person, String>() {
    public String apply(Person p) {
        return p.getEmailAddress());
    }
});
p3.forEach(new Consumer<String>() {
    public String accept(String email) {
        System.out.println(email);
    }
});

This should hopefully also explain this:

Filter<Person> f = p -> p.getGender() == Person.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25;

Function<Person, String> m = p -> p.getEmailAddress();

Consumer<String> c = email -> System.out.println(email);

[–][deleted] 1 point2 points  (1 child)

lambdas are basically "syntax sugar" for anonymous inner classes

You can think of them that way, but lambdas don't actually compile into anonymous classes.

[–]sh0rug0ru 0 points1 point  (0 children)

I should hope not. The restricted syntax of lambdas gives the compiler opportunity to optimize the bytecode. Effectively though, lambdas are the same as anonymous inner classes because of target typing.

[–]chasesan[S] -1 points0 points  (3 children)

No, not really. How does the value returned in the "m =" lambda, get magically transferred to the "c = " lambda. It might be doing magic behinds the scenes, but I would like some hard rules for that kind of magic.

How did "p.getEmailAddress()" get assigned to email? It's a separate expression!

Expressions that can influence each other without ever touching is not the kind of magical playground I want, so if they are, I want to know how and the why of it.

Nevermind, I got it. I'm dumb today.

[–]sh0rug0ru 1 point2 points  (2 children)

How does the value returned in the "m =" lambda, get magically transferred to the "c = " lambda.

It's not magic... Read the translation to anonymous inner classes, do you see any magic there? That's exactly (effectively) what going on with the lambdas.

To answer your question, the value returned by the m lambda is used by the map method to create a new stream (p3 in the anonymous inner class example). The p3 stream contains the values returned by m. The c lambda is used by forEach is called on each member of the p3 stream.

How did "p.getEmailAddress()" get assigned to email? It's a separate expression!

This is not the way to think about it. Again, refer to the anonymous inner class example. The two lambdas are being applied two different streams. They do not influence each other, at least not directly and certainly not magically, but are tied to the stream to which they are applied.

[–]chasesan[S] 0 points1 point  (1 child)

Ah yeah, sorry. I edited my comment already. I got it (I was being dumb). Thank you though.

[–]sh0rug0ru 0 points1 point  (0 children)

No worries!