Collections.unmodifiableList and defensive copy

后端 未结 8 1989
一向
一向 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 04:57

    a1 and a2 will reference the same data (memory).

    the un-mofifiable part comes , only with a2 as entry.

    imaging if you are passing a2 to a method where you expect the method to be idempotent. such cases a2 helps.

    in summary you cant modify data using a2 pointer.

提交回复
热议问题