java.util.Collections類—emptyList()方法的使用
Collections是列表的工具類,其中有好多方便實(shí)用的方法。主要是對(duì)列表的查找、替換、排序、反轉(zhuǎn)等操作。今天介紹一下emptyList()方法的使用,因?yàn)檫@個(gè)方法有一個(gè)大坑!
emptyList()方法的使用
通過java.util.Collections.emptyList()方法的相關(guān)源碼可以得知它實(shí)際上就是返回了一個(gè)空的List,但是這個(gè)List和我們平時(shí)常用的那個(gè)List是不一樣的。這個(gè)方法返回的List是Collections類的一個(gè)靜態(tài)內(nèi)部類,它繼承AbstractList后并沒有實(shí)現(xiàn)add()、remove()等方法,因此這個(gè)返回值List并不能增加刪除元素。
既然這個(gè)List不能進(jìn)行增刪操作,那么它有何意義呢?
這個(gè)方法主要目的就是返回一個(gè)不可變的列表,使用這個(gè)方法作為返回值就不需要再創(chuàng)建一個(gè)新對(duì)象,可以減少內(nèi)存開銷。并且返回一個(gè)size為0的List,調(diào)用者不需要校驗(yàn)返回值是否為null,所以建議使用這個(gè)方法返回可能為空的List。
emptySet()、emptyMap()方法同理。
/**
* The empty list (immutable). This list is serializable.
*
* @see #emptyList()
*/
public static final List EMPTY_LIST = new EmptyList();
/**
* Returns the empty list (immutable). This list is serializable.
*
* <p>This example illustrates the type-safe way to obtain an empty list:
* <pre>
* List<String> s = Collections.emptyList();
* </pre>
* Implementation note: Implementations of this method need not
* create a separate <tt>List</tt> object for each call. Using this
* method is likely to have comparable cost to using the like-named
* field. (Unlike this method, the field does not provide type safety.)
*
* @see #EMPTY_LIST
* @since 1.5
*/
public static final <T> List<T> emptyList() {
return (List<T>) EMPTY_LIST;
}
/**
* @serial include
*/
private static class EmptyList extends AbstractList<Object> implements RandomAccess,Serializable {
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 8842843931221139166L;
public int size() {return 0;}
public boolean contains(Object obj) {return false;}
public Object get(int index) {
throw new IndexOutOfBoundsException("Index: "+index);
}
// Preserves singleton property
private Object readResolve() {
return EMPTY_LIST;
}
}
java.util.Collections.emptyList()方法的測(cè)試
public class CollectionsTest {
public static void main(String[] a) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
System.out.println(list);
list = Collections.emptyList();
System.out.println(list);
list.add(3);
}
}
//執(zhí)行結(jié)果
[1, 2]
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)[]
at com.jiuqi.pay.importfile.test.CollectionsTest.main(CollectionsTest.java:22)
Java Collections.emptyList()方法的注意事項(xiàng)
emptyList()
作用:返回一個(gè)空的List(使用前提是不會(huì)再對(duì)返回的list進(jìn)行增加和刪除操作);
好處:
1. new ArrayList()創(chuàng)建時(shí)有初始大小,占用內(nèi)存,emptyList()不用創(chuàng)建一個(gè)新的對(duì)象,可以減少內(nèi)存開銷;
2. 方法返回一個(gè)emptyList()時(shí),不會(huì)報(bào)空指針異常,如果直接返回Null,沒有進(jìn)行非空判斷就會(huì)報(bào)空指針異常;
注意:此List與常用的List不同,它是Collections類里的靜態(tài)內(nèi)部類,在繼承AbstractList后并沒有實(shí)現(xiàn)add()、remove()等方法,所以返回的List不能進(jìn)行增加和刪除元素操作。
示例:
@Test
public void test1() {
String str = "";
List<String> list = getList(str);
System.out.println(list.size());
}
private static List<String> getList(String str) {
if (StringUtils.isBlank(str)) {
// 使用時(shí)不會(huì)報(bào)空指針
return Collections.emptyList();
// 使用null報(bào)空指針異常
// return null;
}
List<String> list = new ArrayList<String>();
list.add(str);
return list;
}
增刪操作:
@Test
public void test2() {
String str = "abc";
List<String> list = Collections.emptyList();
list.add(str);
System.out.println(list.size());
}
結(jié)果:

如果需要對(duì)collections.emptyList()進(jìn)行增刪操作的話,就需要將collections.emptyList()轉(zhuǎn)換成ArrayList()進(jìn)行操作。
示例:
@Test
public void test2() {
String str = "abc";
List<String> list = Collections.emptyList();
List<String> resultList = new ArrayList<>(list);
resultList.add(str);
System.out.println(resultList.size());
System.out.println(resultList);
}
結(jié)果:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 淺談Java中Collections.sort對(duì)List排序的兩種方法
- Java使用Collections.sort()排序的方法
- java安全之CommonsCollections4詳解
- Java中的Collections類的使用示例詳解
- Java中Collections.sort的使用
- Java的可變參數(shù)與Collections類的功能示例解析
- Java中Collection與Collections的區(qū)別詳解
- Java Collection和Collections的區(qū)別
- Java中的集合工具類Collections詳解
- Java中Collections.sort()排序方法舉例詳解
- Java中Collection和Collections的區(qū)別
相關(guān)文章
Kotlin基本類型自動(dòng)裝箱出現(xiàn)問題解決辦法
這篇文章主要介紹了Kotlin基本類型自動(dòng)裝箱出現(xiàn)問題解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題順利解決,需要的朋友可以參考下2017-10-10
Java 中的 BufferedReader 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
BufferedReader 是緩沖字符輸入流。它繼承于Reader。接下來通過本文給大家介紹BufferedReader的相關(guān)知識(shí),需要的朋友參考下吧2017-05-05
使用GraalVM如何將SpringBoot項(xiàng)目打包成exe
本文介紹了如何使用GraalVM和Maven將Spring Boot項(xiàng)目打包成可執(zhí)行文件的步驟,并詳細(xì)解釋了在打包過程中遇到的常見錯(cuò)誤及其解決方法2024-12-12
Java中利用BitMap位圖實(shí)現(xiàn)海量級(jí)數(shù)據(jù)去重
有許多方法可以用來去重,比如使用列表、集合等等,但這些方法通常只適用于一般情況,然而,當(dāng)涉及到大量數(shù)據(jù)去重時(shí),常見的 Java Set、List,甚至是 Java 8 的新特性 Stream 流等方式就顯得不太合適了,本文給大家介紹了Java中利用BitMap位圖實(shí)現(xiàn)海量級(jí)數(shù)據(jù)去重2024-04-04
netty對(duì)proxy protocol代理協(xié)議的支持詳解
這篇文章主要為大家介紹了netty對(duì)proxy protoco代理協(xié)議的支持詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

