雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

Java編程

當(dāng)前位置:主頁 > 軟件編程 > Java編程 >

Java編程中的HashSet和BitSet詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Java編程|點擊:

Java編程中的HashSet和BitSet詳解

我在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); 

為什么會使用BitSet代替HashSet呢?

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

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

上一篇:Java編程刪除鏈表中重復(fù)的節(jié)點問題解決思路及源碼分享

欄    目:Java編程

下一篇:淺談Java編程ToString()方法重寫的意義

本文標(biāo)題:Java編程中的HashSet和BitSet詳解

本文地址:http://www.jygsgssxh.com/a1/Javabiancheng/8467.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有