Java中關(guān)于size()>0?和isEmpt()的性能考量
size()>0 和isEmpt()性能考量
為何要寫這篇呢?主要是要糾正一個長期以來的誤區(qū):size()>0 一定比isEmpt()性能差。
以下內(nèi)容是社區(qū)里的結(jié)論
- 方法一(數(shù)據(jù)量大,效率低):
if(list!=null && list.size()>0){} - 方法二(數(shù)據(jù)量大,效率高):
if(list!=null && !list.isEmpty()){}
sonar的規(guī)范是這樣描述:
Collection.isEmpty() should be used to test for emptiness
Using Collection.size() to test for emptiness works, but using Collection.isEmpty() makes the code more readable and can be more performant. The time complexity of any isEmpty() method implementation should be O(1) whereas some implementations of size() can be O(n).
明白了吧!
主要是語義更明確,其實判斷List、Map、Set是否為空及效率比較真的沒有多大的必要,確實是沒有大多的提升。看源碼:
ArrayList:
public int size() {
? ? return size;
}
public boolean isEmpty() {
? ? return size == 0;
}HashSet:
public int size() {
? ? return map.size();
}
public boolean isEmpty() {
? ? return map.isEmpty();
}ConcurrentHashMap:
public int size() {
? ? long n = sumCount();
? ? return ((n < 0L) ? 0 :
? ? ? ? ? ? (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
? ? ? ? ? ? (int)n);
}
public boolean isEmpty() {
? ? return sumCount() <= 0L; // ignore transient negative values
}其次,有些時候確實它更快,如果你使用了ConcurrentLinkedQueue、NavigableMap、NavigableSet,看源碼:
ConcurrentSkipListMap
public int size() {
? ? long count = 0;
? ? for (Node<K,V> n = findFirst(); n != null; n = n.next) {
? ? ? ? if (n.getValidValue() != null)
? ? ? ? ? ? ++count;
? ? }
? ? return (count >= Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int) count;
}
public boolean isEmpty() {
? ? return findFirst() == null;
}最后,計算機是門需要刨根問底(點進源碼看看)的技術(shù)活,不能人云亦云。綜上所述,isEmpt的確是更好的選擇。
list.size() > 0 && list != null 和 list != null && list.size()>0區(qū)別
使用場合
list==null;此時list還沒有實例化(new);list.size()==0;此時表明list已經(jīng)實例化了,但list集合里面沒有元素,長度為0
區(qū)別
如果list集合還未實例化,可用list != null && list.size() > 0進行判斷,
如果用list.size() > 0 && list != null 進行判斷的話,會報異常,因為list.size()用 在已經(jīng)實例化的情況下,但現(xiàn)在未實例化,所以出錯;
如果list集合已經(jīng)實例化,則list != null && list.size() > 0 和 list.size() > 0 && list != null 兩者都可以進行判斷。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于遠程調(diào)用RestTemplate的使用避坑指南
這篇文章主要介紹了關(guān)于遠程調(diào)用RestTemplate的使用避坑指南,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
淺談Java回收對象的標(biāo)記和對象的二次標(biāo)記過程
這篇文章主要介紹了淺談Java回收對象的標(biāo)記和對象的二次標(biāo)記過程的相關(guān)內(nèi)容,小編覺得還是挺不錯的,這里給大家分享一下,需要的朋友可以參考。2017-10-10
mybatis?resultMap沒有全部對應(yīng)的字段處理方式
這篇文章主要介紹了mybatis?resultMap沒有全部對應(yīng)的字段處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
java?集合工具類Collections及Comparable和Comparator排序詳解
這篇文章主要介紹了java集合工具類Collections及Comparable和Comparator排序詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06

