Repository navigation
docs: clarify List.of immutability in the array-to-list section - #2941
Open
YangFang1999 wants to merge 1 commit into
Open
YangFang1999 wants to merge 1 commit into
YangFang1999 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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