Java中關(guān)于size()>0?和isEmpt()的性能考量
size()>0 和isEmpt()性能考量
為何要寫這篇呢?主要是要糾正一個(gè)長(zhǎng)期以來(lái)的誤區(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).
明白了吧!
主要是語(yǔ)義更明確,其實(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ū)別
使用場(chǎng)合
list==null;此時(shí)list還沒有實(shí)例化(new);list.size()==0;此時(shí)表明list已經(jīng)實(shí)例化了,但list集合里面沒有元素,長(zhǎng)度為0
區(qū)別
如果list集合還未實(shí)例化,可用list != null && list.size() > 0進(jìn)行判斷,
如果用list.size() > 0 && list != null 進(jìn)行判斷的話,會(huì)報(bào)異常,因?yàn)閘ist.size()用 在已經(jīng)實(shí)例化的情況下,但現(xiàn)在未實(shí)例化,所以出錯(cuò);
如果list集合已經(jīng)實(shí)例化,則list != null && list.size() > 0 和 list.size() > 0 && list != null 兩者都可以進(jìn)行判斷。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot熱加載jar實(shí)現(xiàn)動(dòng)態(tài)插件的思路
本文主要介紹在 Spring Boot 工程中熱加載 jar 包并注冊(cè)成為 Bean 對(duì)象的一種實(shí)現(xiàn)思路,在動(dòng)態(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à)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Boot日志技術(shù)logback原理及配置解析
這篇文章主要介紹了Spring Boot日志技術(shù)logback原理及用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
淺談Springmvc中的頁(yè)面跳轉(zhuǎn)問題
這篇文章主要介紹了淺談Springmvc中的頁(yè)面跳轉(zhuǎn)問題,具有一定參考價(jià)值,需要的朋友可以了解下。2017-12-12
阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏
這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實(shí)現(xiàn)監(jiān)控大屏,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Java實(shí)現(xiàn)中國(guó)象棋的示例代碼
中國(guó)象棋是起源于中國(guó)的一種棋,屬于二人對(duì)抗性游戲的一種,在中國(guó)有著悠久的歷史。由于用具簡(jiǎn)單,趣味性強(qiáng),成為流行極為廣泛的棋藝活動(dòng)。本文將利用Java實(shí)現(xiàn)這一經(jīng)典游戲,需要的可以參考一下2022-02-02
springboot項(xiàng)目啟動(dòng)慢的問題排查方式
這篇文章主要介紹了springboot項(xiàng)目啟動(dòng)慢的問題排查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

