Why can we change the unmodifiable list if we have the original one?

前端 未结 7 718
时光取名叫无心
时光取名叫无心 2020-12-10 15:56

By looking at the code of Collections class, i got to know that when we are using the method unmodifiableList(List list) or unmodifiableCollection(Coll

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 16:20

    Now the point is why it is referring to the same object? Why it don't create a new object?

    Performance. It just doesn't scale to make a full copy. It would be a linear time operation to make a full copy which obviously isn't practical. Also, as others already noted, the point is that you can pass the reference of the unmodifiable list around without having to worry that it gets changed. This is very helpful for multithreaded programs.

提交回复
热议问题