Collections.unmodifiableList and defensive copy

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

    If you need unmodifiable and immutable list or in other words unmodifiable copy of the source list without any dependency to other libraries try this:

    Collections.unmodifiableList(Collections.list(Collections.enumeration(sourceList)))
    

    Collections.list() copies values from its enumeration.

提交回复
热议问题