In the explanation part, what is the meaning of this part:
"While the collection cannot be modified via the unmodifiable view, the underlying collection may still be modified via a direct reference to it. However, the collections returned by the of/ofEntries API methods are in fact unmodifiable."
The text states that methods unmodifiableXXX() creates unmodifiable views, but the underlying collection may still be modified, whereas this is not the case with of/ofEntries. But I tried the code below, using of(), and changed the original arraylist. It did modified the reference from List.of(). So, what is the meaning of "API methods are in fact unmodifiable"?
public static void main(String[] args) {
Collection<Number> col = new HashSet<>();
col.add(1);
var list1 = List.of(col);
col.add(2);
System.out.println(list1); //[[1, 2]]
System.out.println(col == list1.get(0)); //returns true, showing it points to the same object
}
About Question enthuware.ocpjp.v11.2.3407 :
Moderator: admin
-
- Posts: 9
- Joined: Fri Apr 12, 2024 11:09 pm
- Contact:
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3407 :
In your example, list1 points to an immutable List that contains a HashSet pointed to by col. You are adding 2 to col (ie the HashSet) not to the immutable list.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Fri Apr 12, 2024 11:09 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3407 :
So, immutable only means that the I cant add/remove/alter elements point by the immutable reference, but changes in the underlying object (the hashset, in this case) are still reflected in my immutable reference? Except for the copyOf() method?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3407 :
No, first of all, references are not mutable or immutable. Only the objects pointed to by the references can be mutable or immutable. Second, an immutable object doesn't necessarily mean that its members are also immutable. An object may have an instance field of a mutable type.
So, the List object pointed to by list1 is immutable. You can't add or remove elements from this list. You can't do that using any reference because this list object is immutable. However, one of the elements of this list happens to be a mutable Collection. You can add or remove elements to/from that collection.
So, the List object pointed to by list1 is immutable. You can't add or remove elements from this list. You can't do that using any reference because this list object is immutable. However, one of the elements of this list happens to be a mutable Collection. You can add or remove elements to/from that collection.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Apr 25, 2024 3:55 am
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3407 :
That's a great suggestion! References can only point to changeable or immutable things.admin wrote: ↑Thu Apr 25, 2024 2:35 amNo, first of all, references are not mutable or immutable. Only the objects pointed to by the references can be mutable or immutable. Second, an immutable object doesn't necessarily mean that its members are also immutable. An object may have an instance field of a mutable type.
So, the List object pointed to by list1 is immutable. You can't add or remove elements from this list. You can't do that using any reference because this list object is immutable. However, one of the elements of this list happens to be a mutable Collection. You can add or remove elements to/from that collection.
I'm a Recreational Therapist!space bar clicker
-
- Posts: 9
- Joined: Fri Apr 12, 2024 11:09 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3407 :
Thanks for the clarification!
Who is online
Users browsing this forum: No registered users and 21 guests