从JDK 1.5开始, Collections集合工具类中预先定义了一些空集合:
如
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
public static final <K,V> Map<K,V> emptyMap() {
return (Map<K,V>) EMPTY_MAP;
}
使用这些empty方法,可以使代码的可读性变得更好,假设有一个查询方法
public ResultSet query(String tableName, Map<String, Object> queryParams){
}
如果没有任何查询参数,在调用方法query时,我们可以直接用Collections.emptyMap(); 作为参数传递。一下子可以知道没有查询参数。而且没有产生NullPointerException的风险。
同时可以提高某些场景的性能和效率,比如下面这个场景:
验证用户在页面输入的数据,并将所有的错误信息显示在页面上( 需要返回一个List对象)。 如果用户输入的数据都是正确的话,我们可能采用new ArrayList<XXXError>()去处理没有error的场景。在这种情况下,直接直接采用
return Collections.emptyList(); 代替
new ArrayList<XXXError>(),将不需要每次都创建一个新的实例,分配新的空间。
下面添加几个JDK源码中使用emptyXXX方法的例子:
/**
* Returns the list of ObjectNames of MBeans expected to be unregistered
* due to a relation removal (only for relation removal).
*
* @return a {@link List} of {@link ObjectName}.
*/
public List<ObjectName> getMBeansToUnregister() {
List<ObjectName> result;
if (unregisterMBeanList != null) {
result = new ArrayList<ObjectName>(unregisterMBeanList);
} else {
result = Collections.emptyList();
}
return result;
}
ForkJoinPool.java
/**
* Attempts to cancel and/or stop all tasks, and reject all
* subsequently submitted tasks. Tasks that are in the process of
* being submitted or executed concurrently during the course of
* this method may or may not be rejected. This method cancels
* both existing and unexecuted tasks, in order to permit
* termination in the presence of task dependencies. So the method
* always returns an empty list (unlike the case for some other
* Executors).
*
* @return an empty list
* @throws SecurityException if a security manager exists and
* the caller is not permitted to modify threads
* because it does not hold {@link
* java.lang.RuntimePermission}{@code ("modifyThread")}
*/
public List<Runnable> shutdownNow() {
checkPermission();
shutdown = true;
tryTerminate(true);
return Collections.emptyList();
}
原文链接
http://www.wangmengjun.com/showArticleDetail.do?articleId=37
查看更多
http://www.wangmengjun.com/
分享到:
相关推荐
commons-collections-20040616.jar, ...commons-collections.jar, commons-collections3-3.2.1.jar, commons-collections4-4.1-javadoc.jar, commons-collections4-4.1-src.zip, commons-collections4-4.1.jar
Java.Generics.and.Collections.Oct.2006.pdf 完美清晰正版
各个不同 iesi.collections.dll 版本大全,希望能够帮助到需要的人!
列举系统中的环境变量,listView1,System.Collections.DictionaryEntry EnValue in Environment.GetEnvironmentVariables()C#源代码 用VisualStudio2008创建
JSF开发所必需包:花了很长时间才收集好,很费时,现已收集好,何不分享给大家,让大家节省时间做点有意义的事情呢?...commons-collections.jar commons-digester.jar jsf-api.jar jsf-impl.jar jstl.jar standard.jar
Iesi.Collections.dll
Kotlin的不可变集合接口和实现原型。
kotlinx.collections.immutable, Kotlin的不可变集合 Prototype Kotlin的不可变集合库 Kotlin的不可变集合接口和实现 Prototype 。有关详细信息,请参阅建议列表。Prototype实现基于 pcollections ( 版权 2015的作者...
spring.jar, commons-collections.jar, commons-dbcp.jar, commons-pool.jar
本文通过两种方法给大家介绍java集合中的Collections.sort方法对list排序,第一种方式是list中的对象实现Comparable接口,第二种方法是根据Collections.sort重载方法实现,对collections.sort方法感兴趣的朋友一起...
Java Generics and Collections.pdf
NULL 博文链接:https://xuedong.iteye.com/blog/1147254
commons-beanutils-bean-collections.jar commons-beanutils-bean-collections.jar
该文件里包含两个.jar包: collections-generic-4.01.jar和looks-2.1.4.jar, 引入collections-generic-4.01.jar: 右击工程--》Build path ——》Add External JAR-->选中collections-generic-4.01.jar --》OK 在源...
Java Collections.pdf 作者john zukowski