首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java编程中的HashSet跟BitSet

2012-11-07 
Java编程中的HashSet和BitSet作者:chszs,转载需注明。作者博客主页:http://blog.csdn.net/chszs我在Apache

Java编程中的HashSet和BitSet

作者:chszs,转载需注明。
作者博客主页:http://blog.csdn.net/chszs

我在Apache的开发邮件列表中发现一件很有趣的事,Apache Commons包的ArrayUtils类的removeElements方法,原先使用的HashSet现在换成了BitSet。

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呢?

据Apache Commons作者指出,这样代码执行时可以占用更少的内存,速度也更快。




热点排行