Java Collections - 有效搜索日期时间范围
我有一个例子,我有一个表(t1),其中包含类似“
| id | timestamp | att1 | att2 |
现在我必须迭代 att1 类型的元素集合”的项目,并从 t1 获取位于该 att1 的两个特定时间戳之间的所有记录。我必须为单个 att1 执行多次此操作。
因此,为了简化数据库查询,我打算将 t1 中具有特定 att1 属性的每个条目加载到一个集合中,并对该集合执行后续搜索。
是否有一个集合可以处理“2011-02-06 09:00:00”和“2011-02-06 09:00:30”之间的搜索?不保证包含这两个时间戳的条目。
在为此编写实现之前(很可能是一个非常慢的实现^^),我想问你们是否已经有一些现有的集合或者我如何解决这个问题。
谢谢!
I have a case where I have a table (t1) which contains items like
| id | timestamp | att1 | att2 |
Now I have to iterate over a collection of elements of type att1 and get all records from t1 which are between two certain timestamps for this att1. I have to do this operation several times for a single att1.
So in order to go easy on the database queries, I intended to load every entry from t1 which has a certain att1 attribute once into a collection and perform the subsequent searches on this collection.
Is there a collection that could handle a search like between '2011-02-06 09:00:00' and '2011-02-06 09:00:30'? It's not guaranteed to contain entries for those two timestamps.
Before writing an implementation for that (most likely a very slow implementation ^^) I wanted to ask you guys if there might be some existing collections already or how I could tackle this problem.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的。使用 TreeMap ,它基本上是一个排序的key=>value 对的映射及其方法TreeMap::subMap(fromKey, toKey)。
在您的情况下,您将使用时间戳作为地图的键和值att1属性或id或其他对您来说最方便的东西。
Yes. Use TreeMap which is basically a sorted map of key=>value pairs and its method TreeMap::subMap(fromKey, toKey).
In your case you would use timestamps as keys to the map and for values att1 attribute or id or whatever else would be most convenient for you.
我能想到的最接近的(这并不是我真正认为理想的)是编写一个比较器,对日期进行排序,以便范围内的日期计数少于范围之外的日期(比较 < 时始终返回 -1 code>in 与
out
,将in
与in
或out
与比较时为 0 out
,并且在将out
与in
进行比较时始终返回 +1然后,使用此比较器对集合进行排序(我建议使用
ArrayList
)。 。不过,您最好编写自己的过滤器(我推荐使用
LinkedList
),对其进行迭代,然后删除任何内容 不在范围内,保留一个主副本,以便在需要时生成新的副本以传递到过滤器中。The closest I can think of, and this isn't really what I would consider ideal, is to write a comparator that will sort dates so that those within the range count as less than those outside the range (always return -1 when comparing
in
toout
, 0 when comparingin
toin
orout
toout
, and always return +1 when comparingout
toin
.Then, use this comparator to sort a collection (I suggest an
ArrayList
). The values within the range will appear first.You might just be better off writing your own filter, though. Input a collection (I recommend a
LinkedList
), iterate over it, and remove anything not in the range. Keep a master copy around for spawning new ones to pass into the filter, if you need to.您可以在集合中创建您想要的对象,我认为是 att1,实现 Comparable 接口,然后使用compareTo 方法比较时间戳字段。有了这个,它就可以在任何排序的集合中工作,例如树集,从而可以轻松地迭代和提取特定范围内的所有内容。
You can make the object you want in your collection, which I think is att1, implement the Comparable interface and then have the compareTo method compare the timestamp field. With this in place it will work in any sorted collection, such as a treeSet, making it easy to iterate and pull out everything in a certain range.