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 要兼容,另一方面代码习惯,明了比简洁还要重要。

相关推荐
A懿轩A2 小时前
【Maven 构建工具】从零到上手 Maven:安装配置 + IDEA 集成 + 第一个项目(保姆级教程)
java·maven·intellij-idea
野犬寒鸦2 小时前
从零起步学习并发编程 || 第一章:初步认识进程与线程
java·服务器·后端·学习
我爱娃哈哈2 小时前
SpringBoot + Flowable + 自定义节点:可视化工作流引擎,支持请假、报销、审批全场景
java·spring boot·后端
XiaoFan0122 小时前
将有向工作流图转为结构树的实现
java·数据结构·决策树
love530love2 小时前
技术复盘:llama-cpp-python CUDA 编译实战 (Windows)
人工智能·windows·python·llama·aitechlab·cpp-python·cuda版本
小突突突3 小时前
浅谈Java中的反射
java·开发语言
Anastasiozzzz3 小时前
LeetCode Hot100 295. 数据流的中位数 MedianFinder
java·服务器·前端
我真的是大笨蛋3 小时前
Redo Log详解
java·数据库·sql·mysql·性能优化
索荣荣3 小时前
Java动态代理实战:从原理到精通
java·开发语言
兩尛3 小时前
c++的数组和Java数组的不同
java·开发语言·c++