• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Lambdas

 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am having a tough time understanding how lambdas work. I understood lambdas are methods without names that can be passed around like a variable.
In the above code,  

check( (h,k) -> h>1,5);  // line A

 Can anyone explain how is this passed and how it works ?In check method "i" has got 5 as value but Climb "ex" got some address. I thought the type should be boolean as h>1 should return a boolean value. I am not getting how lambdas work. Each example has something new
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to know and learn more about predicates, lambdas and functional interfaces, you should definitely have a look at these topics:
  • Doubt about lambda in Chapter 4 Question 27 pg 230 (Java OCA 8 Programmer I Study Guide, Sybex) (this one covers a similar code snippet like the one you've posted with a very detailed explanation)
  • Predicates in Lambda
  • 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
     
    Vidya Shivram
    Ranch Hand
    Posts: 39
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Reol,

    Your explanation in the first link you posted was really helpful. I still need wants these things to be clear
    * Lamdas are essentially an object on which abstract methods of the functional interface are invoked right?
    In the below example, I am trying to do with lambdas and without lambdas comparison.



    I have commented without lambda code. According to my understanding, above code should work but it is throwing an error in line B saying there are unimplemented methods. what is wrong in my understanding?
     
    Vidya Shivram
    Ranch Hand
    Posts: 39
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The code worked when I removed class implementing the interface statement.
    So here, lambdas can be used to call methods of functional interface from a class which is not implementing that interface.

    Thanks, Reol
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Probably you made some kind of mistake in overriding the method of the Climb1 interface, because there's no restriction that a class can't implement a functional interface.

    The following code snippet compiles successfully
     
    Saloon Keeper
    Posts: 12492
    269
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Chivid Ram wrote:* Lamdas are essentially an object on which abstract methods of the functional interface are invoked right?


    Almost. Lambdas are expressions that return something that looks like an object on which methods of the functional interface can be invoked. The expression part is important, because no functional objects are created until the expression is actually evaluated.
     
    Vidya Shivram
    Ranch Hand
    Posts: 39
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Probably you made some kind of mistake in overriding the method of the Climb1 interface because there's no restriction that a class can't implement a functional interface.



    Ya .. In the previous code, I had commented interface implementation. If I have to implement abstract method then I can simply create the object of that class and call the method. what's the need of Lamdas here?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 12492
    269
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lambdas are simply a much shorter way of writing down the same thing. They get rid of a lot of boilerplate code that gets in the way of reading the actual important stuff.
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Chivid Ram wrote:If I have to implement abstract method then I can simply create the object of that class and call the method. what's the need of Lamdas here?


    Write less code! Get rid of boilerplate code! Be concise & focus on what really matters!

    Compare three different alternatives
     
    I promise I will be the best, most loyal friend ever! All for this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic