Java編程中的HashSet和BitSet詳解

Java編程中的HashSet和BitSet詳解

創(chuàng)新互聯(lián)建站主要從事網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)房山,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792

我在Apache的開發(fā)郵件列表中發(fā)現(xiàn)一件很有趣的事,Apache Commons包的ArrayUtils類的removeElements方法,原先使用的HashSet現(xiàn)在換成了BitSet。

HashSet<Integer> toRemove = new HashSet<Integer>(); 
for (Map.Entry<Character, MutableInt> e : occurrences.entrySet()) { 
  Character v = e.getKey(); 
  int found = 0; 
  for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { 
    found = indexOf(array, v.charValue(), found); 
    if (found < 0) { 
      break; 
    } 
    toRemove.add(found++); 
  } 
} 
 
 
return (char[]) removeAll((Object)array, extractIndices(toRemove)); 

新代碼如下:

BitSet toRemove = new BitSet(); 
for (Map.Entry<Character, MutableInt> e : occurrences.entrySet()) { 
  Character v = e.getKey(); 
  int found = 0; 
  for (int i = 0, ct = e.getValue().intValue(); i < ct; i++) { 
    found = indexOf(array, v.charValue(), found); 
    if (found < 0) { 
      break; 
    } 
    toRemove.set(found++); 
  } 
} 
return (char[]) removeAll(array, toRemove); 

為什么會(huì)使用BitSet代替HashSet呢?

據(jù)Apache Commons作者指出,這樣代碼執(zhí)行時(shí)可以占用更少的內(nèi)存,速度也更快。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

文章標(biāo)題:Java編程中的HashSet和BitSet詳解
網(wǎng)址分享:http://muchs.cn/article18/gpjddp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、定制開發(fā)響應(yīng)式網(wǎng)站、建站公司、營(yíng)銷型網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司