5

I need more clarification about lambda expression. How 'p' represents List<Person> people? Could you explain clear to me

List<Person> people = new ArrayList<>();
people.add(new Person("Mohamed", 69));
people.add(new Person("Doaa", 25));
people.add(new Person("Malik", 6));
Predicate<Person> pred = (p) -> p.getAge() > 65;

2 Answers 2

7

No, p is not a List<Person> but a Person.

Predicate<Person> pred = p -> p.getAge() > 65;

This lambda expression declares 1 formal parameter and returns a boolean. As such, it can be represented as a Predicate (because the Predicate interface has a single functional method with this exact signature, called test). The type of p will be determined by the type of the Predicate you are creating.

For example, in the following code, p will be a List<Person>:

Predicate<List<Person>> predicate = p -> p.isEmpty();
Sign up to request clarification or add additional context in comments.

2 Comments

can you explain about (p) in the code and how iteration happen here?
@KannanThangadurai (p), the same as p, (the parenthesis are not needed in this case) is the formal parameter of the lambda expression. I suggest you read this Oracle tutorial on lambda expressions.
1

Lambdas in java it is just a syntax shugar for anonymous classes

Code from you example is equal to

List<person> people = new ArrayList<>();
people.add(new Person("Mohamed", 69));
people.add(new Person("Doaa", 25));
people.add(new Person("Malik", 6));

Predicate<person> pred = new Predicate<person>() {
    public boolean test(person p) {
        return p.getAge() > 65;
    }
}

To simplify syntax in java you can skip type declaration in lambda expression and add only name of value, like you did.

Predicate<person> pred = (p) -> p.getAge() > 65;

Or if you want, you can write something like this

Predicate<person> pred = (person p) -> p.getAge() > 65;

Its to be noted you can skip type declaration only if it can be counted from lambda code somehow. For example

Comparator<String> comp
= (firstStr, secondStr) // Same as (String firstStr, String secondStr)
    -> Integer.compare(firstStr.length(),secondStr.length());

4 Comments

Lambdas are not syntactic sugar for anonymous classes. Lambda expressions and anonymous classes share some common use cases but that’s all. For these common use cases you can write an anonymous class that is equivalent to the lambda expression basically doing the same thing. But they are not the same thing.
And what is the differencein java? In bytecode we will get anonimous class anyway, isn't it?
@user2982622, absolutely not. And even aside from bytecode they are different (lambdas are stateless, have different lexical scope and so on).
As pointed out by Tagir, there are semantic differences and a different lexical scope on source code level, i.e. lambdas can’t define instance variables nor invoke methods of the functional interface they’re going to implement and this and super have a different meaning. Further, there are some practical differences at runtime and big differences on the byte code level

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.