java - Searching LinkedHashMap, faster method than sequential? -
i wondering if there more efficient method getting objects out of linkedhashmap timestamps greater specified time. i.e. better following:
iterator<foo> = foo_map.values().iterator(); foo foo; while(it.hasnext()){ foo = it.next(); if(foo.get_timestamp() < minstamp) continue; break; }
in implementation, each of objects has 3 values: "id," "timestamp," , "data." objects insterted in order of timestamps, when call iterator on set, ordered results (as required linked hashmap contract). map keyed object's id, can lookup them id.
when them timestamp condition, however, iterator sorted results. improvement on generic hashmap, still need iterate sequentially on of range until find next entry higher timestamp specified one.
since results sorted, there algorithm can pass iterator (or collection to), can search faster sequential? if went treemap alternative, offer overall speed advantages, or doing same thing in background? since collection sorted insertion order already, i'm thinking tree map has lot more overhead don't need?
there no faster way ... if use linkedhashmap
.
if want faster access, need use different data structure. example, treeset
appropriate comparator might better solution aspect of problem. example if treeset ordered date, calling tailset
appropriate dummy value can give elements greater or equal given date.
since results sorted, there algorithm can pass iterator (or collection to), can search faster sequential?
not linkedhashmap
.
however, if ordered list arraylist
instead, use "binary search" on list ... provided lock prevent concurrent modifications while searching. (actually, concurrency potential issue consider no matter how implement ... including current linear search.)
if want keep ability id
lookups, need 2 data structures; e.g. treeset
, hashmap
share element objects. treeset
more efficient trying maintain arraylist
in order assuming there random insertions and/or random deletions.
Comments
Post a Comment