4

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;
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Kannan Thangadurai
  • 1,117
  • 2
  • 17
  • 36

2 Answers2

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();
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • can you explain about (p) in the code and how iteration happen here? – Kannan Thangadurai Nov 11 '15 at 10:51
  • 2
    @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](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html) on lambda expressions. – Tunaki Nov 11 '15 at 10:55
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());
user2982622
  • 103
  • 1
  • 6
  • 4
    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. – Holger Nov 11 '15 at 11:17
  • 1
    And what is the differencein java? In bytecode we will get anonimous class anyway, isn't it? – user2982622 Nov 11 '15 at 12:30
  • 4
    @user2982622, absolutely not. And even aside from bytecode they are different (lambdas are stateless, have different lexical scope and so on). – Tagir Valeev Nov 11 '15 at 12:37
  • 1
    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](http://stackoverflow.com/questions/27524445/does-a-lambda-expression-create-an-object-on-the-heap-every-time-its-executed/27524543#27524543) and [big differences on the byte code level](http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html) – Holger Nov 11 '15 at 14:27