About Question enthuware.ocpjp.v8.2.1801 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
lexer24
Posts: 6
Joined: Wed Jun 22, 2016 8:06 am
Contact:

About Question enthuware.ocpjp.v8.2.1801 :

Post by lexer24 »

Hi,

Could you please explain me why java.util.Stream method void forEach(Consumer<? super T> action) is aplicable for public StringBuilder append(String str) method of StringBuilder class? So this becomes legal:
messages.stream().forEach(s->s.append("helloworld"));
As for me, append method returns reference to StringBuilder but forEach expects Comsumer which means void return type.

If we dig deeper then we will come to the following legal code:

Code: Select all

...
Consumer<StringBuilder> cm = s -> s.append("helloworld");
Function<StringBuilder, StringBuilder> r3 = s -> s.append("helloworld");
...
How does Java manage this?

Best regards,
Aleksei

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1801 :

Post by admin »

Returns type of append has no relation to the type that forEach expects. Think of it these three steps -
1. The Java compiler creates a dummy inner class of the type that is expected by forEach (i.e. Consumer, in this case).
2. It adds a method to this dummy class. This method implements the abstract method of the Consumer interface.
3. It adds the code that you write in your lambda expression into the method that it created in step 2.

So forEach(s->s.append("asdf")), gets converted into:

Code: Select all

forEach( new Consumer(){
  public void accept(StringBuilder s){
    s.append("asdf");
  }
}
You have asked a very basic question about lamdas. You should have learnt this for OCAJP 8 itself. So I will suggest you to read this topic from a good book or follow Java Tutorial on lambdas. You can also read this short article to begin with: http://enthuware.com/index.php/home/115


HTH,
Paul.

lexer24
Posts: 6
Joined: Wed Jun 22, 2016 8:06 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1801 :

Post by lexer24 »

Hi,

Thank you for the answer.
Don't get me wrong I know how lambdas work.
As for me, in my example there is an ambiguous situation, because lambda:

Code: Select all

 s -> s.append("helloworld");
could be treated either as such

Code: Select all

 s ->{ s.append("helloworld");}
or

Code: Select all

s -> {return s.append("helloworld");}
And that was the question, how does Java know which one to choose while applying the lambda?
Will try also to find this information in documentation. Thank you again.

Best regards,
Aleksei

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1801 :

Post by admin »

Compiler makes this decision based on the funtional interface applicable in the given situation. Here, the interface is Consumer, whose functional method doesnt return anything.

dongyingname
Posts: 18
Joined: Sat Jun 22, 2019 4:10 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1801 :

Post by dongyingname »

Code: Select all

List<StringBuilder> messages = Arrays.asList(new StringBuilder(), new StringBuilder()); 
messages.stream().forEach(s->s.append("helloworld")); 
    messages.forEach(s->{ 
    s.insert(5,",");     System.out.println(s);
});
.stream() makes a copy of underlying List, which is messages. Why could the .forEach() eventually affect the original messages?
Could you please also provide simple examples where pipeline doesn't affect the original List?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1801 :

Post by admin »

1. Where did you read that .stream() makes a copy of underlying List??
2. Assuming for a second that even if it made copy of the list (it does not), the objects in the list are the same that you get the in the stream. So, the call to s.append will modify the same object.
3. If you don't want to affect elements of the original list, you need to make a deep copy first. See this: https://stackoverflow.com/questions/478 ... ect=1&lq=1

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests