Skip to content

docs: clarify List.of immutability in the array-to-list section - #2941

Open
YangFang1999 wants to merge 1 commit into
Snailclimb:mainfrom
YangFang1999:docs/list-of-immutable
Open

YangFang1999 wants to merge 1 commit into
Snailclimb:mainfrom
YangFang1999:docs/list-of-immutable

Conversation

@YangFang1999

Copy link
Copy Markdown

Closes #2649

按 #2940 中的方案,澄清「数组转集合」一节中 List.of() 的不可变语义:

小节标题由 ArrayList 改为 List(第 6 种方法返回的是不可变集合,不总是 ArrayList)
方法 6 标题标注「不可变」
补充说明:List.of() 返回 java.util.ImmutableCollections 的实现,add/remove/set 会抛出 UnsupportedOperationException,包含 null 元素会抛出 NullPointerException
补充与 Arrays.asList() 的区别:后者为定长列表,可通过 set 替换元素;List.of() 完全不可变
补充需要可变集合时的写法:new ArrayList<>(List.of(array)) 之后可正常增删
已按 #2940 的验证清单核对:三种修改操作均抛 UnsupportedOperationException,null 元素抛 NullPointerException。

Refs #2940

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

关于数组转集合方法的第六种:使用Java9的List.of()

1 participant