Java Collections - 有效搜索日期时间范围

发布于 2024-10-31 20:03:37 字数 407 浏览 1 评论 0原文

我有一个例子,我有一个表(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪装你 2024-11-07 20:03:37

是的。使用 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.

佞臣 2024-11-07 20:03:37

我能想到的最接近的(这并不是我真正认为理想的)是编写一个比较器,对日期进行排序,以便范围内的日期计数少于范围之外的日期(比较 < 时始终返回 -1 code>in 与 out,将 ininout比较时为 0 out,并且在将 outin 进行比较时始终返回 +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 to out, 0 when comparing in to in or out to out, and always return +1 when comparing out to in.

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.

浅浅淡淡 2024-11-07 20:03:37

您可以在集合中创建您想要的对象,我认为是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文