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).
明白了吧!
主要是語義更明確,其實(shí)判斷List、Map、Set是否為空及效率比較真的沒有多大的必要,確實(shí)是沒有大多的提升??丛创a:
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 }
其次,有些時(shí)候確實(shí)它更快,如果你使用了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; }
最后,計(jì)算機(jī)是門需要刨根問底(點(diǎn)進(jìn)源碼看看)的技術(shù)活,不能人云亦云。綜上所述,isEmpt的確是更好的選擇。
list.size() > 0 && list != null 和 list != null && list.size()>0區(qū)別
使用場合
list==null;
此時(shí)list還沒有實(shí)例化(new);list.size()==0;
此時(shí)表明list已經(jīng)實(shí)例化了,但list集合里面沒有元素,長度為0
區(qū)別
如果list集合還未實(shí)例化,可用list != null && list.size() > 0進(jìn)行判斷,
如果用list.size() > 0 && list != null 進(jìn)行判斷的話,會報(bào)異常,因?yàn)閘ist.size()用 在已經(jīng)實(shí)例化的情況下,但現(xiàn)在未實(shí)例化,所以出錯;
如果list集合已經(jīng)實(shí)例化,則list != null && list.size() > 0 和 list.size() > 0 && list != null 兩者都可以進(jìn)行判斷。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot熱加載jar實(shí)現(xiàn)動態(tài)插件的思路
本文主要介紹在 Spring Boot 工程中熱加載 jar 包并注冊成為 Bean 對象的一種實(shí)現(xiàn)思路,在動態(tài)擴(kuò)展功能的同時(shí)支持在插件中注入主程序的 Bean 實(shí)現(xiàn)功能更強(qiáng)大的插件2021-10-10關(guān)于spring 掃描不到j(luò)ar中class文件的原因分析及解決
這篇文章主要介紹了關(guān)于spring 掃描不到j(luò)ar中class文件的原因分析及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Spring Boot日志技術(shù)logback原理及配置解析
這篇文章主要介紹了Spring Boot日志技術(shù)logback原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏
這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09