About Question enthuware.ocpjp.v8.2.1877 :

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

Moderator: admin

Post Reply
__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

About Question enthuware.ocpjp.v8.2.1877 :

Post by __JJ__ »

Hi

Doesn't

Code: Select all

f.apply(vowels);
just create a subList object, then discard it, in the same way as

Code: Select all

"abc".concat("def");
does? I got the answer right but from reading the provided explanation I'm not sure if I got it right for the right reasons.
TIA.

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

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

Post by admin »

No, "abc".concat("def") creates a new independent string altogether.
f.apply(vowels); returns only a view of the original list. It doesn't return an independent list. If you modify this list the original will get modified. This is explained in detail in the explanation with an example.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey »

Hello.

Code: Select all

Remember that, however, if you modify the sub list, the changes will be visible in the original list. 
For example, the following will print aeioxu:

List<String> view = f.apply(vowels); //get a view backed by the original list 
view.add("x"); //modify the view 
vowels.forEach(System.out::print);  //updates visible in original list
Why the output is aeioxu and not aeioux?
I think "add" method should add element at the end of the "view" list. Where is a mistake?

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

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

Post by admin »

Because the view is created using list.subList(2, 4); and you are adding to the end of this view.
If you like our products and services, please help us by posting your review here.

syzygy
Posts: 3
Joined: Wed Mar 31, 2021 2:56 pm
Contact:

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

Post by syzygy »

The answer is correct but the reason given is wrong
The answer is correct because apply returns the result of the function call and this result is discarded

Code: Select all

List<String> vowels = new ArrayList<String>();
vowels.add("a");
vowels.add("e");
vowels.add("i");
vowels.add("o");
vowels.add("u");
Function<List<String>, List<String>> f = list -> list.subList(2, 4);
vowels= f.apply(vowels); // MODIFIED
vowels.forEach(System.out::print);
would print

Code: Select all

io
it has nothing to do with subList returning a view

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

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

Post by admin »

The explanation addresses a mistaken belief that that list.subList(2, 4); modifies the list pointed to by list and the explanation is correct. f = list -> list.subList(2, 4); creates a new list with certain elements from the original list but doesn't modify the original list. This is true. The code prints the original list. And therefore, prints the same values that the original list had.

The explanation clearly says, "therefore, when you print the elements from the original list after calling subList". So, it says the same thing that you are saying. The code is not printing the new list.

Your code: vowels= f.apply(vowels); is entirely different. It doesn't not modify the original list either. It simply makes the vowel point to a new list.

In fact, your scenario is explained in the last part of the explanation where the new list is modifed but changes are also visible in the original list.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests