Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

OCP certification - Question 11 Enthuware Test 1: Lambda function

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have OCP certification Question (- Enthuware), I thought that I knew how lambda works, but now I'm confused again. The question is what will it/print. The answer 300.0. Ok. reasonable.



What I do not understand or not can follow is next:
  • 1. //2 there is ComputeIfPresent with ("222", bif); Which I think have two arguments 1: "222", 2: lambda expression. Correct me if I'm wrong.
  • 2. Now for me the big point: how are these translated to a1 and a2??? This is a miracle for me. Ok due to the functional interface for BiFunction. A 'normal' method call with arguments, I easily can follow, but this suddenly a1 has the value of "222" and bank account reference.
  • 3. And what for me is amazing at line //1 a new instance of Q11_BankAccount is created, but were/how is it added to the HashMap myAccts???  


  • What I'm missing? This is so abra-cadabra for me, ghost software seems it to me. I hope that someone can explain this to me, what the clue is and were I'm going wrong

     
    Bartender
    Posts: 3705
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please check this JavaDoc

    And this pseudo-code is the answer to your question:

     
    Enthuware Software Support
    Posts: 4540
    45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The key pieces of information that you need to solve this question are as follows (both given in the explanation already)

    1. What does the Map's computeIfPresent method do exactly:  Briefly, it takes a key, checks if the map already contains that key, and if yes, then it replaces that value in the map with the new value generated by the BiFunction. So, basically, the key will be mapped with a new value.

    and

    2. How it applies to the given situation: Since myAccts map does contain a key "222", the computeIfPresent method will execute the function and replace the existing value associated with the given key in the map with the new value returned by the function.

    Nico van de Kamp wrote:Hello,
    1. //2 there is ComputeIfPresent with ("222", bif); Which I think have two arguments 1: "222", 2: lambda expression. Correct me if I'm wrong.


    Yes, 222 and a lambda expression are the two arguments. But more importantly, you need to realize that the arguments are - 1. a String value that potentially exists as a key in the map, and 2. a lambda expression that implements a BiFunction that takes the key (a String value), the value currently associated with that key (an Account object) in the map, and returns a new value (also an Account object) that will be put in the same map against that same key.
    (This is given in the JavaDoc description of the computeIfPresent/Absent methods and also in the explanation.)


    2. Now for me the big point: how are these translated to a1 and a2??? This is a miracle for me. Ok due to the functional interface for BiFunction. A 'normal' method call with arguments, I easily can follow, but this suddenly a1 has the value of "222" and bank account reference.


    That's because you are invoking myAccts.computeIfPresent("222", bif); The computeIfPresent method internally takes the first argument "222" and passes it on to the apply method of the BiFunction as a1. It also uses the first argument "222", gets the currently associated value in the map and passes it on as the second argument to the apply method as a2.


    3. And what for me is amazing at line //1 a new instance of Q11_BankAccount is created, but were/how is it added to the HashMap myAccts???  


    The computeIfPresent method does it internally. It executes the BiFunction's apply method with the two arguments as described above, and then it replaces the existing value mapped to "222" with the new value returned by the BiFunction.


    I would suggest you to go through a book to understand how lambdas (and also method references) work in general because they do confuse beginners a lot initially. Start with simple examples initially to get the hang of it and then it will all seem very intuitive.
    Going straight away to the mock questions (which are expert level as expected by the OCP 11 exam) may leave one thoroughly confused because this really is not easy to grasp for beginners.

    HTH,
    Paul.
     
    Nico van de Kamp
    Greenhorn
    Posts: 11
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Paul,

    I have read chapter 3 again and although it seems more or less still magic -"abacadabra" to me, understand it.

    I understand that:

    the key value is retrieved from the with the value, while the lambda herewith is called and placed in the Bifunction "arguments:
    =

    That computeIfPresent this is doing I was not aware of it and it still magic to me. But I hope that I can recognize these situations.

    Thanks!

    Nico
     
    Nico van de Kamp
    Greenhorn
    Posts: 11
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello Paul,

    I have read chapter 3 again and although it seems more or less still magic -"abacadabra" to me, understand it.

    I understand that:

    the key value is retrieved from the with the value, while the lambda herewith is called and placed in the Bifunction "arguments:
    =

    That computeIfPresent this is doing I was not aware of it and it still magic to me. But I hope that I can recognize these situations.

    Thanks!

    Nico
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic