方法注释:
/** * Returns an immutable list containing only the specified object. * The returned list is serializable. * * @param <T> the class of the objects in the list * @param o the sole object to be stored in the returned list. * @return an immutable list containing only the specified object. * @since 1.3 */
这个方法主要用于只有一个元素的优化,减少内存分配,无需分配额外的内存。
普通写法:
List<MyBean> beans= MyService.getInstance().queryBean(param); if (CollectionUtils.isEmpty(beans)) { beans= new ArrayList<>(); MyBean bean= new MyBean(param); beans.add(bean); }
优化写法:
List<MyBean> beans= MyService.getInstance().queryBean(param); if (CollectionUtils.isEmpty(beans)) { MyBean bean= new MyBean(param); beans= Collections.singletonList(bean); }
类似地还有:
public static <T> Set<T> singleton(T o); public static <T> List<T> singletonList(T o); public static <K,V> Map<K,V> singletonMap(K key, V value); // 或者直接调用常量 EMPTY_LIST public static final <T> List<T> emptyList(); //或者直接调用常量 EMPTY_MAP public static final <K,V> Map<K,V> emptyMap(); //或者直接调用常量 EMPTY_SET public static final <T> Set<T> emptySet()
需要注意的是,以上6个方法返回的容器类均是immutable,即只读的,如果调用修改接口,将会抛出UnsupportedOperationException
相关推荐
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
commons-collections-3.2.1.jar 希望有用!
Java.Generics.and.Collections.Oct.2006.pdf 完美清晰正版
各个不同 iesi.collections.dll 版本大全,希望能够帮助到需要的人!
JSF开发所必需包:花了很长时间才收集好,很费时,现已收集好,何不分享给大家,让大家节省时间做点有意义的事情呢?...commons-collections.jar commons-digester.jar jsf-api.jar jsf-impl.jar jstl.jar standard.jar
列举系统中的环境变量,listView1,System.Collections.DictionaryEntry EnValue in Environment.GetEnvironmentVariables()C#源代码 用VisualStudio2008创建
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
该文件里包含两个.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
commons-beanutils-bean-collections.jar commons-beanutils-bean-collections.jar