Page 1 of 1
About Question enthuware.ocpjp.v8.2.1877 :
Posted: Fri Jul 27, 2018 2:45 pm
by __JJ__
Hi
Doesn't
just create a subList object, then discard it, in the same way as
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.
Re: About Question enthuware.ocpjp.v8.2.1877 :
Posted: Fri Jul 27, 2018 9:47 pm
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.
Re: About Question enthuware.ocpjp.v8.2.1877 :
Posted: Sat Mar 23, 2019 8:57 am
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?
Re: About Question enthuware.ocpjp.v8.2.1877 :
Posted: Sat Mar 23, 2019 10:35 am
by admin
Because the view is created using list.subList(2, 4); and you are adding to the end of this view.
Re: About Question enthuware.ocpjp.v8.2.1877 :
Posted: Wed Mar 31, 2021 3:01 pm
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
it has nothing to do with subList returning a view
Re: About Question enthuware.ocpjp.v8.2.1877 :
Posted: Thu Apr 01, 2021 2:17 am
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.