好用的Trove简介

trove是轻量级实现java.util Collections API的第三方开源项目
官网: http://trove.starlight-systems.com/overview

trove相比jdk原生的集合类有三个优势:
1、更高的性能
2、更底的内存消耗
3、除了实现原生Collections API并额外提供更强大的功能

集合是java编程最常用的API之一,把项目的集合对象改用trove替换就能获得性能提升和内存的节省。
这是对一个项目底成本的优化方案。

下面二段代码分别测试trove和原生的集合的性能
1. trove
     
public void testIterator(){
             TIntObjectMap map = new TIntObjectHashMap();
            
             for( int i = 0; i < 1000; i++){
                  Game g = new Game();
                  g.setName( "最终幻想" + i);
                  g.setSize(15000 + (i << 3));
                  g.setCtDate( new Date());
                   map.put(i, g);
            }
            
             int size = map.size();
             TIntObjectIterator it = map.iterator();
             for( int i = size; i > 0; i--){
                  it.advance();
                  System. out.println(it.key() + "=" + it.value());
                  
                   if(it.key() == 3){
                        Game g = new Game();
                        g.setName( "最终幻想13");
                        g.setSize(15000 + (i << 3));
                        g.setCtDate( new Date());
                         it.setValue(g);
                  }
            }
            
            System. out.println( "=======================================" );
            System. out.println(map.get(3));
      }

2. HashMap
public void testIterator(){
             Map map = new HashMap();
            
             for( int i = 0; i < 1000; i++){
                  Game g = new Game();
                  g.setName( "最终幻想" + i);
                  g.setSize(15000 + (i << 3));
                  g.setCtDate( new Date());
                   map.put(i, g);
            }
            
             Set set = map.entrySet();
             for( Iterator it = set.iterator(); it.hasNext(); ){
                   Entry e = (Entry )it.next();
                  System. out.println(e.getKey() + "=" + e.getValue());
                  
                   if(((Integer)e.getKey()).intValue() == 3){
                        Game g = new Game();
                        g.setName( "最终幻想13");
                        g.setSize(18000);
                        g.setCtDate( new Date());
                         e.setValue(g);
                  }
            }
            
            System. out.println( "=======================================" );
            System. out.println(map.get(3));
      }

两段的测试结果,trove花费0.001s,HashMap花费0.090s
trove的测试代码使用其Map额外提供的iterator进行遍历,当然他也支持通过entrySet方式遍历。

使用SizeOf.jar计算上面两段的map占用内存的大小,trove占用214024byte,HashMap占用250128byte,可见内存方面节省的比率并是很大。

从官方下载的API中可以看中trove根据基础的数据类型实现了Map、List和Set,比如TIntObjectHashMap其key只能是int, value是继承Object的所有对象;TIntList只能存入int;TIntSet只能存入int。

在java里有些对象的hashCode和equals方法不能被覆盖(比如:String和数组),以这种对象为Map的key时就不能随心所欲,trove的提供的custom map可以解除这种约束,只需要实现HashingStrategy接口传递给Map,Map判断key是否相同时调用HashingStrategy的computeHashCode和equals代替object自身的hashCode和equals,以下是官网一个例子

For example, this code:

char[] foo, bar; 
foo = new char[] {'a','b','c'}; 
bar = new char[] {'a','b','c'}; 
System.out.println(foo.hashCode() == bar.hashCode() ? "equal" : "not equal"); 
System.out.println(foo.equals(bar) ? "equal" : "not equal");


produces this output:

not equal
not equal

And so an entry stored in a java.util.HashMap with foo as a key could not be retrieved with bar, since there is no way to override hashCode() or equals() on language array objects.

In a gnu.trove.THashMap, however, you can implement a TObjectHashingStrategy to enable hashing on arrays:
class CharArrayStrategy implements TObjectHashingStrategy { 
    public int computeHashCode(Object o) { 
        char[] c = (char[])o; 
        // use the shift-add-xor class of string hashing functions 
        // cf. Ramakrishna and Zobel, "Performance in Practice 
        // of String Hashing Functions" 
        int h = 31; // seed chosen at random 
        for (int i = 0; i < c.length; i++) { // could skip invariants 
            h = h ^ ((h << 5) + (h >> 2) + c[i]); // L=5, R=2 works well for ASCII input 
        } 
        return h; 
    } 

    public boolean equals(Object o1, Object o2) { 
        char[] c1 = (char[])o1; 
        char[] c2 = (char[])o2; 
        if (c1.length != c2.length) { // could drop this check for fixed-length keys 
            return false; 
        } 
        for (int i = 0, len = c1.length; i < len; i++) { // could skip invariants 
            if (c1[i] != c2[i]) { 
                return false; 
            } 
        } 
        return true; 
    } 
} 


总结:
Trove是很值得了解和使用的一个开源项目,你会喜欢上他的。

你可能感兴趣的:(java)