本讲解读 Java 9+ 中的不可变集合:工厂方法
List.of、
Set.of、
Map.of 与
Map.ofEntries。我们将它们与包装器
Collections.unmodifiableList 对比,讨论限制(禁止修改、禁止
null、集合元素与 Map 键禁止重复)、元素顺序与性能。还会说明
Map.of 的
10 对限制以及常见异常:
UnsupportedOperationException、
NullPointerException、
IllegalArgumentException。另外给出实践示例,展示这些集合如何简化设计并提升读操作的线程安全性。