简体   繁体   中英

Correct Data Structure for Collection with Multiple Mutable Look-Up Properties

If I have an in memory collection of 5,000 or more objects of type SampleObject

class SampleObject {
  ...
  Date getLastRefreshDate()
  Status getCurrentStatus()
}

I want to quickly get the sub-list of objects with a refresh date that is older than some value, and also be able to quickly get the objects with a certain status, what data-structure/algorithm would be useful? Is iterating the list and doing comparisons fast enough? What if the list grows to 25,000 or more?

A TreeMap<Date, SampleObject> could do the job of getting "older than" a certain date, pretty easily -- you'd just use headMap to get all the objects older than some value.

You'd need a separate Map<Status, List<SampleObject>> (or, if you can use third-party libraries, a Guava Multimap ) to track objects with some particular status, though, but I think a second data structure is inevitable anyway if you're not willing to pay for linear search.

The NavigableSet and NavigableMap classes offer methods to do just that.

NavigableSet already offer methods like headSet and tailSet to get all elements before or after a other given element. You could use your date criteria as a Comparator if not already povided as a natural order for your SampleObject class.

Besides other useful methods like lower , floor , ceiling , and higher .

Likewise NavigableMap offer similar methods like headMap and tailMap to do exactly the same kind of slicing.

get the sub-list of objects with a refresh date that is older than some value, and also be able to quickly get the objects with a certain status

Sounds like a k -dimensional range or search problem. Your options include:

You might be able to get away with lineary access via a sorted structure, if you mostly return data in one dimension only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM