您的位置:首页 > 其它

Trove 集合类学习

2015-06-05 10:48 302 查看
trove把java原生的集合类范型去掉,而用具体的类型集合来取代。这造成了类型比较多,但是同时针对具体类型优化后的效果比java原生集合类还是效果上要好些。

对HashMap<Integer,Integer>和TIntIntHashMap做了一个对比,

public static void main(String args[]) throws JSONException {
long start = System.currentTimeMillis();
//        System.gc();
long initSize = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
TIntIntHashMap map = new TIntIntHashMap();
//        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
int i = 0;
while (i++ < 1000000) {
map.put(i, i);
}
//        System.gc();
long afterUseSize = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
System.out.println("cost time:" + (System.currentTimeMillis() - start));
System.out.println("use memory:" + (afterUseSize - initSize));
}
发现i=10000时,HashMap<Integer,Integer>内存使用情况比TIntIntHashMap差,但是时间开销上要好。

i=1000000时,TIntIntHashMap在内存上和时间开销上都明显好于HashMap<Integer,Integer>,说明在集合元素较多情况下,trove的hashmap比java原生map效果更好。

这主要因为trove做hash时采用的开放探测选址,java原生map采用的是链表。前者不用保存链表节点,所以内存占用较少,速度较快。但是前者如果没有足够的空间索引,则效率会很低。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: