简体   繁体   English

具有多个可变查找属性的正确数据结构以进行收集

[英]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 如果我在内存中有5,000个或更多SampleObject类型的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? 如果列表增加到25,000或更多怎么办?

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. TreeMap<Date, SampleObject>可以很容易地获得“早于”某个日期的工作-您只需使用headMap即可使所有对象早于某个值。

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. 您需要一个单独的Map<Status, List<SampleObject>> (或者,如果可以使用第三方库,一个Guava Multimap )来跟踪具有某些特定状态的对象,但是我认为第二个数据结构是不可避免的无论如何,如果您不愿意为线性搜索付费。

The NavigableSet and NavigableMap classes offer methods to do just that. NavigableSetNavigableMap类提供了实现此目的的方法。

NavigableSet already offer methods like headSet and tailSet to get all elements before or after a other given element. NavigableSet已经提供诸如headSettailSet类的方法来获取另一个给定元素之前或之后的所有元素。 You could use your date criteria as a Comparator if not already povided as a natural order for your SampleObject class. 如果尚未将其日期条件用作SampleObject类的自然顺序,则可以将其用作Comparator

Besides other useful methods like lower , floor , ceiling , and higher . 除了其他有用的方法,如lower floorfloorceilinghigher

Likewise NavigableMap offer similar methods like headMap and tailMap to do exactly the same kind of slicing. 同样, NavigableMap提供类似headMap和tailMap的方法来进行完全相同的切片。

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. 听起来像是k维范围或搜索问题。 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. 如果您大多只返回一个维度的数据,则可以通过排序结构摆脱线性访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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