Page 1 of 1
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Mon Feb 03, 2020 8:39 am
by Sergey
I think the right answer should be [1], [1, 2]. Because "The list/set returned by the of/copyOf methods is completely independent of the original collection".
At the line 1 we create the first independent copy of HashSet which contains 1.
Then (at line 2) we add 2 in the HashSet.
Then (at line 3) we create the second independent copy of HashSet which contains 1,2
And then we print "results" of two independent copies of HashSet.
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Mon Feb 03, 2020 8:53 am
by admin
>Then (at line 3) we create the second independent copy of HashSet which contains 1,2
So, if the original has 1, 2, why do you think the copy will not have 1, 2?
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Mon Feb 03, 2020 6:45 pm
by Sergey
I thought, the first independent copy of HashSet (at line //1) has 1.
And the second independent copy (at line //3) has 1,2.
I thought when we added 2 to HashSet (at line //2) we changed only original, but not the first independent copy.
I mean, when we add 2 to HashSet we change only HashSet and the second copy, because second copy will be made after that moment. I thought the first independent copy still has only 1
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Sun Mar 22, 2020 12:07 pm
by tape88
admin wrote: ↑Mon Feb 03, 2020 8:53 am
>Then (at line 3) we create the second independent copy of HashSet which contains 1,2
So, if the original has 1, 2, why do you think the copy will not have 1, 2?
"The list/set returned by the of/copyOf methods is completely independent of the original collection. Thus, if you modify the original collection after passing it to of/copyOf methods, those changes will not be reflected in the list returned by the of/copyOf methods."
Why //1 updates after //2 ?

Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Sun Mar 22, 2020 11:44 pm
by admin
You missed this part of the explanation:
The of methods accept either an array or multiple individual parameters. If you pass it a collection, it will be treated as a regular object i.e. it will return a list/set containing the same collection object instead of returning a list/set containing the objects that the passed collection contains.
So, the list1 created at //1 does not contain the same elements as the collection pointed to by col. It contains the same Collection object pointed to by col.
Therefore, when you add an element to that collection object at //2, you will see the print out from list1 change even though the List pointed to by list1 has not really changed.
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Thu Nov 05, 2020 8:09 am
by arnestone
list1 is a immutable list of COLLECTIONS. So you can't add or remove COLLECTIONS, but the collections in the list can be mutable.
List1 is in fact a list with one member, the HashSet of numbers.
So a change where another Collection<Number> is added to the original collection will not reflect after the line 1.
The change that is made at line 2 will not be added to list1 but the change at line 3 will.
Code: Select all
Collection<Number> col = new HashSet<>();
Collection<Number> col2 = new HashSet<>();
col.add(1);
var list1 = List.of(col); //1
System.out.println(list1);
col.addAll(col2); //2
col.add(2); //3
System.out.println(list1);
Special thanks Tiago Ventosa who explained this to me

Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Thu Apr 15, 2021 9:07 am
by waraging
why is the answer in an 2-d array
[[1, 2]], [1, 2]
instead of the normal array
[1, 2], [1, 2]
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Thu Apr 15, 2021 9:19 am
by admin
It is not a 2 D array. It is a list of collections. Please go through the discussion above.
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Thu Jan 25, 2024 8:06 pm
by gosheymj
Unless I'm misreading/misunderstanding, item #2 in your "Explanation" for this questions seems misleading.
2. The list/set returned by the of/copyOf methods is completely independent of the original collection. Thus, if you modify the original collection after passing it to of/copyOf methods, those changes will not be reflected in the list returned by the of/copyOf methods.
While that comment is clearly accurate with respect to
List.copyOf(Collection col), it does not seem to apply to
List.of(Collection col), as demonstrated by the correct response, which prints
[1,2] for
list1 even though the value
2 was added after the list was returned.
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Fri Jan 26, 2024 12:14 am
by admin
You are misunderstanding.
var list1 = List.of(col); //1
In the above line of code, the Collection pointed to by list1 is not backed by the Collection pointed to col. Actually, the Collection pointed to by col itself becomes an element in the list. In this case, the type of list will be deduced as Collection<Number> (because type of col is Collection<Number>). So, you can't really add a Number to list1 (i.e. you can't do list1.add(1)). But you do this: list1.add(new List<Number()); But this element will not go in the collection pointed to by col.
This shows that the Collection pointed to by list1 has nothing to do with the Collection pointed to by col.
Of course, the Collection pointed to by col itself is mutable and so you can add elements to that collection. That is why when you print list1, you see updated elements of col (because list1 contains col).
This is what the explanation also says:
The of methods accept either an array or multiple individual parameters. If you pass it a collection, it will be treated as a regular object i.e. it will return a list/set containing the same collection object instead of returning a list/set containing the objects that the passed collection contains.
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Fri Jan 26, 2024 4:16 pm
by gosheymj
Thanks for the reply. Please review the below to clarify where I'm going wrong. Thanks.
1. Your explanation indicates that
if you modify the original collection after passing it to of/copyOf methods, those changes will not be reflected in the list returned by the of/copyOf methods
.
2. By "original collection" I assume we mean the HashSet object created and assigned to "col".
3. The original collection (col) is passed to List.of() in order to create/return list1.
4. The original collection (col) is modified (a '2' is added to it).
5. The modification to the original collection now appears in the print out of the list returned by List.of() (i.e., list1, which prints [1,2]).
The above result appears to contradict the quoted statement above.
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Fri Jan 26, 2024 9:55 pm
by admin
>The modification to the original collection now appears in the print out of the list returned by List.of() (i.e., list1, which prints [1,2]).
Yes, the modification appears in the printout but that is because the printout prints the collection not because this new collection is modified.
Consider this:
Code: Select all
StringBuffer sb = new StringBuffer():
list.add(sb);
print(list); //prints nothing
sb.append("abc");
print(list); //prints abc
Does this mean list has been modified with the new member abc? No. The list still contains just the same StringBuffer element.
Now consider this code:
Code: Select all
List a = new ArrayList();
a.add("1");
System.out.println(a.size()); //prints 1
List b = Collections.synchronizedList(a);
System.out.println(b.size()); //prints 1
a.add("2");
System.out.println(b.size()); //prints 2
The above code shows what it means by changes being reflected to the other collection. Adding an element to the old collection (pointed to by a) works as if you added an element to the new collection (pointed to by b).
This does not happen with List.of method.
Even so, I see that the statement that you have quoted is causing confusion and we will update it to make it clear.
thanks for the feedback!
Re: About Question enthuware.ocpjp.ii.v11.2.3302 :
Posted: Sat Jan 27, 2024 10:24 am
by gosheymj
Thanks again- I appreciate the cordial tone of the discussion. I understand and agree with all you've said, and further with your comment that it may be worth updating the quoted statement. In particular, the portion that indicates
...those changes will not be reflected in the list returned by the of/copyOf methods...
because in fact the two lists behave differently and in my opinion it'd be helpful to more clearly call that out. For example (and as you've already clarified) if the following code were appended to the end of the example in the original question:
Code: Select all
col.add(77);
System.out.println(list1+", "+list2);
it produces the subsequent output [[1, 2,
77]], [1, 2], clarifying that whereas list1 is a List<Collection<Number>> with a single element (that
is subject to updates to that element), list2 is a List<Number> containing precisely the two elements sourced from the original collection and is
not subject to subsequent updates to the original collection since it no longer has any connection to it.
Thanks again for your time!