java - TreeSet Comparator -
i have treeset , custom comparator. values server according changes in stock
ex: if time=0 server send entries on stock (unsorted) if time=200 server send entries added or deleted after time 200(unsorted)
in client side sorting entries. question more efficient
1> fetch entries first , call addall method or 2> add 1 one
there can millions of entries.
/////////updated///////////////////////////////////
private static map<integer, keywordinfo> hashmap = new hashmap<integer, keywordinfo>(); private static set<integer> sortedset = new treeset<integer>(comparator); private static final comparator<integer> comparator = new comparator<integer>() { public int compare(integer o1, integer o2) { int integercomparevalue = o1.compareto(o2); if (integercomparevalue == 0) return integercomparevalue; keywordinfo k1 = hashmap.get(o1); keywordinfo k2 = hashmap.get(o2); if (null == k1.getkeyword()) { if (null == k2.getkeyword()) return integercomparevalue; else return -1; } else { if (null == k2.getkeyword()) return 1; else { int comparestring = alphanumericcmp.comparator.compare(k1.getkeyword().tolowercase(), k2.getkeyword().tolowercase()); //int comparestring = k1.getkeyword().compareto(k2.getkeyword()); if (comparestring == 0) return integercomparevalue; return comparestring; } } } };
now there event handler gives me arraylist of updated entries, after adding them hashmap calling
final map<integer, keywordinfo> maptoreturn = new submap<integer, keywordinfo>(sortedset, hashmap);
i think bottleneck can more network-related cpu related. bulk operation fetching new entries @ once more network efficient.
with regards cpu, time required populate treeset not change consistently between multiple add()s , addall(). reason behind treeset relies on abstractcollection's addall() (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/util/abstractcollection.java#abstractcollection.addall%28java.util.collection%29) in turn creates iterator , calls multiple times add().
so, advice on cpu side is: choose way keeps code cleaner , more readable. obtained through addall().
Comments
Post a Comment