[英]Java - Search a List of Objects for a date between 2 dates
I have a sorted List of objects(From is Sorted): List LocationDetail我有一个排序的对象列表(来自已排序): List LocationDetail
---------------------------------------------------------------------------------------------
| id | Place | Name | From | To | Address
---------------------------------------------------------------------------------------------
| 2 | Office | Mark | 2020-06-02 08:00:00 | 2020-06-02 19:59:59 | Office_Address |
| 4 | Office | John | 2020-06-02 08:00:00 | 2020-06-02 19:59:59 | Office_Address |
| 1 | Home | Mark | 2020-06-01 20:00:00 | 2020-06-02 07:59:59 | Home_Address |
| 3 | Home | John | 2020-06-01 20:00:00 | 2020-06-02 07:59:59 | Home_Address |
---------------------------------------------------------------------------------------------
I want to search details for the following:我想搜索以下内容的详细信息:
Place, Name, locationTime地点、名称、位置时间
If locationTime falls between From and To, Return the row.如果 locationTime 介于 From 和 To 之间,则返回该行。 ( Place, Name, From and To combination is Unique )
(地点、名称、从和到的组合是唯一的)
My Solution:我的解决方案:
My Question:我的问题:
Is there a better solution?有更好的解决方案吗?
Since you don't need to keep duplicates in your list, I would try to trade it for a TreeSet
or a TreeMap
, which provide methods like subset(fromElement, toElement)
and submap(fromKey, toKey)
respectively -- two flavours each, declared in SortedSet/NavigableSet and SortedMap/NavigableMap, so you can control, if needed, whether the from and two are inclusive or exclusive.由于您不需要在列表中保留重复项,因此我会尝试将其
TreeSet
或TreeMap
,它们分别提供了subset(fromElement, toElement)
和submap(fromKey, toKey)
之类的方法——每种都有两种风格,在 SortedSet/NavigableSet 和 SortedMap/NavigableMap 中声明,因此您可以在需要时控制 from 和两者是包含还是排除。
It is not exactly straightforward (but still worth pursuing), because:这并不简单(但仍然值得追求),因为:
LocationDetail
objects to use as "from" and "to" in your queryLocationDetail
对象以在查询中用作“from”和“to”flatMap
on the returned submap(from, to).values().stream()
submap(from, to).values().stream()
上执行一些flatMap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.