Trove 集合类学习

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采用的是链表。前者不用保存链表节点,所以内存占用较少,速度较快。但是前者如果没有足够的空间索引,则效率会很低。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值