Collections.unmodifiableList and defensive copy

后端 未结 8 1958
一向
一向 2020-12-13 04:30

If I write

List a1 = Arrays.asList(1, 2, 3);
List a2 = Collections.unmodifiableList(a1);

a2 is r

8条回答
  •  时光取名叫无心
    2020-12-13 05:08

    The statement:

    Collections.unmodifiableList(a1);
    

    returns a wrapper over the original collection whose modifier methods throw UnsupportedOperationException.

    The wrapper is read-through, meaning that if you modify a1, the changes reflect on the wrapped collection a2.

提交回复
热议问题