• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Predicates in Lambda

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why cant I pass a MyLambdaExample1 object in line1 . Compiler says, type Object is expected. MyLambdaExample1 is also an Object, right.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Why cant I pass a MyLambdaExample1 object in line1 . Compiler says, type Object is expected. MyLambdaExample1 is also an Object, right.

Because of the type of reference variable p1. The type is Predicate without a generic parameter and then the type of the parameter is considered to be Object (the mother class of all objects). So having Predicate is equivalent with Predicate<Object>. If you change the declaration of reference variable p1 to Predicate<Object> the code snippet still won't compile. And that might be a bit confusing, because MyLambdaExample1 IS-A Object. But as you probably know, this code snippet won't compile neitherThe generic parameter at both sides have to be exactly the same and can't be polymorphic. Otherwise you'll get a compiler error.

And that's exactly the same with your example: if you define String as the generic parameter of Predicate, the compiler expects the type of the parameter to be String as well. And if you use Object or CharSequence as the generic parameter, the parameter itself must be Object or CharSequence respectively (and not one of its subclasses or implementing classes. These will therefore fail to compileBut these predicates will successfully compile, because the types are the same

And for completeness: when you have passed the OCAJP exam and you'll prepare for the OCPJP exam, you'll have to know a lot about generics (including some real intricacies ). One of them is the bounded wildcard and here is a code example (which compiles successfully)So using a bounded wildcard you can use any subclass of Object as parameter. But once again: this is definitely not on the OCAJP exam, so you don't have to worry about it

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Its difficult to relate this to predicates, cos they seem very complicated. But I guess, I will have to remember it this way.

Predicates will accept only the type defined in the Predicate object, they cannot accept subclasses.

<? extends Object> , lets us define any subclasses of object in the predicates,thats interesting.

Thank you for the prompt reply Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Its difficult to relate this to predicates, cos they seem very complicated. But I guess, I will have to remember it this way.

It's actually not that difficult with a bit of background. A lambda expression likeis the shorthand syntax forI think we can both agree that writing the lambda expression is much easier and faster to type. And now you should be able to see much better how this is related withAnd why the following code snippet won't compile (just as with the ArrayList code example)

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats using Anonymous inner class. we cannot instantiate the Predicate interface.We are just referring to the object that implements the interface , through the inner class and also overriding the test method.now its clear.

Lambda expression is faster to type.But with inner class its much better understand, because you actually see whats happening.

There was another doubt I had...how are we using the Predicate interface, test method in our class without implementing it. The answer should be, we are actually creating an anonymous inner class that implements the Predicate interface and overriding it in the lambda expression.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Thats using Anonymous inner class. we cannot instantiate the Predicate interface.We are just referring to the object that implements the interface , through the inner class and also overriding the test method.now its clear.

Indeed! The above code snippet is using an anonymous inner class which is definitely not on the OCAJP exam. I have used it for demonstration purposes only, to illustrate what's happening behind the scenes.

Ramya Subraamanian wrote:There was another doubt I had...how are we using the Predicate interface, test method in our class without implementing it. The answer should be, we are actually creating an anonymous inner class that implements the Predicate interface and overriding it in the lambda expression.

To create a lambda expression, you need a functional interface! A functional interface is any interface that contains only one abstract method. A functional interface may contain one or more default or static methods. The Predicate interface is definitely a functional interface (as it only has the abstract test method). That's why you can write the lambda expression: the compiler knows everything about the interface. Because the compiler knows the type of the parameter(s) of the method, you can write a lambda expression without even mentioning the parameter typeAnd the compiler also knows the name of the method, so that's why you are allowed to write this codeAnd finally, the compiler knows that everything behind the -> operator should be used as the implementation of the method (with some intricacies, like adding a return statement with a single statement).

If you want to know and learn more about functional interfaces, you should definitely have a look at these topics:
  • lambdas in Boyarsky and Selikoff book
  • clarification in chapter 5 predicates in page 214 (Java OCA 8 Programmer I Study Guide, Sybex)
  • could we say 'out' is inner class of 'System' class ?


  • Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ramya Subraamanian wrote:how are we using the Predicate interface, test method in our class without implementing it. The answer should be, we are actually creating an anonymous inner class that implements the Predicate interface and overriding it in the lambda expression.


    I thought so, because the lambda expression is like anonymous class implementation. Maybe the idea is somewhat similar to this !

    That's why you can write the lambda expression: the compiler knows everything about the interface. Because the compiler knows the type of the parameter(s) of the method, you can write a lambda expression without even mentioning the parameter type


    Agreed.

    I should have said Predicate is a "functional interface".

    One of the threads you mentioned, was created by me. Had a good time, reading it again.

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic