Collections.unmodifiableList and defensive copy

后端 未结 8 1994
一向
一向 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:51

    If you were looking to keep a1 mutable and make an immutable copy of it without Guava, this is one way you could do it.

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

提交回复
热议问题