JDK 9 List.of(...)

复制代码
		// JDK 9      写法: List.of(...)
//		List<Point> polygon = List.of(new Point(0, 0), new Point(0, 4), new Point(4, 4), new Point(4, 0));

		// JDK 1.6 写法:使用ArrayList并手动add元素
		List<Point> polygon = new ArrayList<Point>();
		polygon.add(new Point(0, 0));
		polygon.add(new Point(0, 4));
		polygon.add(new Point(4, 4));
		polygon.add(new Point(4, 0));
html 复制代码
JDK 9 List.of(...)

JDK 1.6            boolean add(E e);
复制代码
/**
     * Appends the specified element to the end of this list (optional
     * operation).
     *
     * <p>Lists that support this operation may place limitations on what
     * elements may be added to this list.  In particular, some
     * lists will refuse to add null elements, and others will impose
     * restrictions on the type of elements that may be added.  List
     * classes should clearly specify in their documentation any restrictions
     * on what elements may be added.
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *         is not supported by this list
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws NullPointerException if the specified element is null and this
     *         list does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this list
     */
    boolean add(E e);

没有什么为什么,一方面以前做的项目1.6 1.8 要兼容,另一方面代码习惯,明了比简洁还要重要。

相关推荐
程序员阿明14 分钟前
idea把插件啥的不默认放在C盘
java·ide·intellij-idea
linweidong1 小时前
中科闻歌Java面试题及参考答案
java·python·大模型·提示词·ai agent·mcp·rag原理
OpenAnolis小助手1 小时前
一句话看透 JVM,SysOM 诊断 Skill 新增 Java 应用诊断能力
java·开发语言·jvm·阿里云·操作系统控制台·sysom
ForteScarlet1 小时前
Kotlin 2.4.20 现已发布,新特性多不多?
android·java·开发语言·开源·kotlin·jetbrains
萧瑟余晖1 小时前
Java深入解析篇六十二之安全机制详解
java·开发语言
小刘在重生~2 小时前
Java 常用类|包装类 装箱拆箱、Integer 缓存面试题
java·python·缓存
Orange_sparkle2 小时前
Pi Agent vs LangGraph:从人机交互、企业 RAG 到持久化工作流的完整选型指南
java·网络·人机交互
我是Superman丶3 小时前
Arduino IDE 安装教程:Windows/macOS/Linux 开发环境搭建
ide·windows·macos
Wang's Blog3 小时前
Java框架快速入门: Spring Security+OAuth2之过滤器与过滤器链
java·开发语言·spring
谢尔登4 小时前
01_Java基础速通
java·开发语言