简体   繁体   English

Java - 在对象列表中搜索两个日期之间的日期

[英]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:我的解决方案:

  1. Extracted the List of property From to a List fromTimes将属性列表从时间提取到列表
  2. Wrote a custom binary search implementation: method LocalDateTime[] getFirstLastIndex(List fromTimes, LocalDateTime locationTime) This method returns the first and last index of the locationTime (for duplicate values(From))写了一个自定义的二分查找实现:方法 LocalDateTime[] getFirstLastIndex(List fromTimes, LocalDateTime locationTime) 这个方法返回locationTime的第一个和最后一个索引(对于重复值(From))
  3. Search the List If firstIndex > listSize/2 firstIndex = firstIndex, LastIndex = listSize-1 If firstIndex < listSize/2 firstIndex = firstIndex, LastIndex = listSize-1 (Also the same)搜索列表 If firstIndex > listSize/2 firstIndex = firstIndex, LastIndex = listSize-1 If firstIndex < listSize/2 firstIndex = firstIndex, LastIndex = listSize-1 (也一样)

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.由于您不需要在列表中保留重复项,因此我会尝试将其TreeSetTreeMap ,它们分别提供了subset(fromElement, toElement)submap(fromKey, toKey)之类的方法——每种都有两种风格,在 SortedSet/NavigableSet 和 SortedMap/NavigableMap 中声明,因此您可以在需要时控制 from 和两者是包含还是排除。

It is not exactly straightforward (but still worth pursuing), because:这并不简单(但仍然值得追求),因为:

  • with a Set you need to create two LocationDetail objects to use as "from" and "to" in your query使用 Set 您需要创建两个LocationDetail对象以在查询中用作“from”和“to”
  • with a Map (keyed by the "from" value), you need to handle the case where multiple location details have the same "from" time.使用 Map(由“from”值键入),您需要处理多个位置详细信息具有相同“from”时间的情况。 So you will probably need to store some Collection of location details for each "from" value and do some flatMap on the returned submap(from, to).values().stream()因此,您可能需要为每个“from”值存储一些位置详细信息集合,并在返回submap(from, to).values().stream()上执行一些flatMap

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

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