最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
JDK9中Set.of方法的使用注意事项
时间:2026-06-02 16:25:01 编辑:袖梨 来源:一聚教程网
JDK9引入的Set.of()方法在创建集合时严格禁止重复元素,这一特性开发者需要特别注意。下面通过具体示例说明其使用规范。
Set.of()使用注意
public class Set1Demo { static void main() {
Set set = Set.of(1, 1, 2, 3, 4, 5);
System.out.println(set);
}
}
执行上述代码将直接触发异常

总结
使用Set.of()方法时必须确保元素唯一性,这是JDK9为该API设定的强制性约束条件。